Why say using HOC's otpions in the recompose pattern is not very reusable?
See original GitHub issueFrom the docs https://www.apollographql.com/docs/react/recipes/recompose.html#other, it says
Normaly if you need to add sideffect to mutate function, you would manage it in HOC’s options ->props part by doing something like { mutate: () => mutate().then(sideEffectHandler) }. But that is not very reusable. Using recompose’s withHandlers() you can compose same prop manipulation in any count of components.
then i follow the given link to a blog post, it demonstrates the use case when dealing with mutation.
export default compose(
injectIntl,
graphql(mutation),
withHandlers({
toggleMutation: ({ mutate, _id, intl }: { mutate: Function, _id: string, intl: Object }): Function => {
return (): Promise<any> => {
return mutate({
variables: {
someId: _id,
},
}).then((data: string) => {
console.log(data, 'Return value');
}).catch((e: Object) => {
console.error(e, 'Error');
});
};
}
})
)(MutationButton);
and i found the syntax is quite cumbersome. Why don’t just
export default compose(
injectIntl,
graphql(mutation, {
props: ({ mutate }) => ({
toggleMutation: (_id: string, intl: Object) => mutate!({
variables: { id, provider },
}).then((data: string) => {
console.log(data, 'Return value');
}).catch((e: Object) => {
console.error(e, 'Error');
});
}),
}),
)(MutationButton);
Could the docs elaborate more on the benefit of using withHandlers()?
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Using Recompose to write clean higher-order components
Learn how Recompose methods can help create cleaner HOCs and how it simplifies the development and organization of React components.
Read more >Functional Components with Recompose | by Thulio Philipe
This article will show a different way to create React components. We'll discuss responsibilities, HOCs, functional components and Recompose ...
Read more >Why is the syntax for higher order components in react often of ...
I speculate that one reason this is so common is to make composition easier. If you are applying multiple HoCs to a single...
Read more >React Hooks, my introduction - Zenika
Why is it so exciting? · The end of the segmentation between functional and class components, at least in the same project. ·...
Read more >Tips to replace Recompose's HOCs with React Hooks for ...
Too many HOCs are less maintainable ... Recompose is a library bringing you a set of useful HOC(higher-order components) utility functions for ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, just decoupling. Examples, well, there could be 2 UI components calling same mutation, but you want to track 2 different Google Analytics actions. So you reuse the graphql mutation (it could have options other than props so you dont wanna copypaste 20 lines of vaariables and other computation parts raw into compose, but prepare some
withClickMutation
HoC) and you can prepare separate HoC which you use like this withGASideffect(“clicked first button”). And for 2nd component you can still usewithClickMutation
HoC, but this time compose it with withGASideffect(“clicked second button”)awesome! thanks @ShockiTV