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.
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
Most helpful comment
The main case where
withHandlersis still needed in combination withwithStateHandlersis the updating of some internal state and calling the upper handler likeAs forwarding event via
withStateHandlersis not a good idea, as it will be called async.