-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(hoc): adding hoc support #1
Conversation
Hi @mfrachet, I'd also like to discuss the signature of the HoC, the current implementation works like this: // define a component
const Example = ({ loading, error, data }) => {
if (loading) return "Loading...";
if (error) return "An error has occurred";
const { module, instance } = data;
return <div>1 + 2 = {instance.exports.add(1, 2)}</div>;
};
// wrap it with the HoC
const WasmExample = withWasm(Example);
// we can use it like this
<WasmExample url="/add.wasm" {...otherProps} /> I think we might also consider an alternative solution that allows us to create HoCs that use wasm modules. In this way we can create, for example, libraries or modules that exports them instead of a component that provides constants urls and so on. // define a component
const Example = ({ loading, error, add }) => {
if (loading) return "Loading...";
if (error) return "An error has occurred";
const { module, instance } = data;
return <div>1 + 2 = {add(1, 2)}</div>;
};
// define an HoC based on our helper
// withWasm(
// wasmPropsProvider = props => props,
// mapProps = props => props
// )
const withAdd = withWasm(
// callback that takes the props and returns the ones used to instantiate the Wasm component
(/* props */) => ({
url: '/add.wasm'
}),
// map Wasm-provided props to the ones to provide to the decorated component
({ loading, error, data }) => ({
loading,
error,
add: data && data.instance.exports.add
})
);
// wrap the component with the HoC
const WasmExample = withAdd(Example);
// we can use it like this
<WasmExample {...otherProps} /> With this signature, using the default values, we can also achieve the same behaviour that you have provided: const WasmExample = withWasm()(Example);
<WasmExample url="/add.wasm" {...otherProps} /> What do you think about it? Does it make sense? |
I'll merge and publish a new release soon! Thank you! |
[email protected] is now online 🎉 Thank you! |
In the React universe, when we try to aggregate behaviors or avoid computation repetition, we use to rely on 3 possibilites:
This PR is a suggestion to add a function
withWasm
that allows to wrap a component inside theWasm
one using the HoC approach.