Reconfigure with Handlers ... asynchronously?

Is it possible / safe to use withHandlers with promises? Example:.

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

Thank!

+4
source share
2 answers

After some tests, I realized that it works very well. Recompose rocks to create with clean components.

It works great and works very 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);

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

+3
source

But there are some problems when we use it as follows:

compose(
  withState('loginStatus', 'setLoginStatus', {loading: false, error:null}),
  withHandlers({
    loginUserMutation: props => async args => {
      try {
        props.setLoginStatus({loading: true, error: null});
        await props.loginUser(args);
      } catch(error) {
        props.setLoginStatus({...props.loginStatus, error});
      } finally {
        props.setLoginStatus({...props.loginStatus, loading: false});
      }
    }
  })
)

props await props.loginUser(args). , .

, .


0

Source: https://habr.com/ru/post/1667042/


All Articles