question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why say using HOC's otpions in the recompose pattern is not very reusable?

See original GitHub issue

From 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:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ShockiTVcommented, Dec 11, 2017

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 use withClickMutation HoC, but this time compose it with withGASideffect(“clicked second button”)

0reactions
goldenbearkincommented, Dec 13, 2017

awesome! thanks @ShockiTV

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found