Recompose: withStateHandlers does not allow for partially applied functions

Created on 8 Nov 2017  路  6Comments  路  Source: acdlite/recompose

A common pattern I'll use with withHandlers is to write partially applied functions. e.g:

withHandlers({
  onSetActivePopup: props => name => () => ...
})
<Avatar onClick={onSetActivePopup('user')} />

However, it appears you cannot make use of this pattern when using a method returned via withStateHandlers.

Most helpful comment

The main case where withHandlers is still needed in combination with withStateHandlers is the updating of some internal state and calling the upper handler like

withStateHandlers(..., { updateState: ...}),
withHandlers({
  onChange: ({onChange, updateState}) => e => {
    onChange(e);
    updateState(blabla);
  }
})

As forwarding event via withStateHandlers is not a good idea, as it will be called async.

All 6 comments

It uses the same pattern as withHandlers.

And it can't return partial functions as it's return is a state. This is written in docs.

@istarkov I'm a bit confused by the presence of this issue as the usage of withStateHandlers is actually quite different to that of the withState withHandlers combination. Curious why withStateHandlers would be recommended over them?

withState have a lot of pain with flow.
withStateHandlers can be used with flow easily.
withState in non functional form have all the issues of non functional form of react setState.
withStateHandlers always use a functional form of React setState

In all my cases I prefer withStateHandlers over withState, mostly because of flow and that it allows (not in all cases) to simplify combination of withStateHandlers and withState.

The main case where withHandlers is still needed in combination with withStateHandlers is the updating of some internal state and calling the upper handler like

withStateHandlers(..., { updateState: ...}),
withHandlers({
  onChange: ({onChange, updateState}) => e => {
    onChange(e);
    updateState(blabla);
  }
})

As forwarding event via withStateHandlers is not a good idea, as it will be called async.

Thanks @istarkov. Just thinking out loud, what are the disadvantages of having the method return a function that returns state, e.g:

withStateHandlers({
  incrementOn: ({ counter }) => value => event => ({
    counter: counter + value,
  }),

Currently the event is handled alongside value (introduced here): https://github.com/acdlite/recompose/pull/476/files

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nemocurcic picture nemocurcic  路  3Comments

cdomigan picture cdomigan  路  4Comments

xialvjun picture xialvjun  路  4Comments

rndmerle picture rndmerle  路  3Comments

uriklar picture uriklar  路  4Comments