Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recompose withHandlers ... asynchronously?

Is is possible/safe to use withHandlers with promises? Ex.:

withHandlers({
    onChange: props => event => {
      props.callAPI(props.data)
        .then(data => props.updateData(data))
    },
...

Thanks!

like image 296
DavidC Avatar asked Jan 17 '17 17:01

DavidC


1 Answers

After some tests I realized that it's working pretty well. Recompose rocks for building with pure components.

This is perfectly valid and working pretty well.

const enhWithHandlers = withHandlers({
  loginUserMutation: props => args => {
    props.updateMutationState(loading: true, error: null });
    props.loginUser(args)
      .then(() =>
        props.updateMutationState({loading: false, error: null }))
      .catch(err =>
        props.updateMutationState({ loading: false, error: err }));
  }
},
...
// then compose like
export default compose(
  reduxConnect,
  gqlConnectLogin,
  gqlConnectRegister,
  enhWithState,
  enhWithHandlers
)(UserLoginRegister);

It helps me to overcome lack of ability to reflect results of graphQl mutation with Apollo client to the wrapped component. This handles it perfectly and without the need of side effects in the component itself.

like image 75
DavidC Avatar answered Sep 30 '22 09:09

DavidC