useReducer
Syntax
useReducer(reducer, initialState): [state, dispatch]
In many cases useState and useAtom are sufficiently capable of dealing with the state of components, especially when separated into many smaller hooks with a single responsibility. However, sometimes you will have to deal with more complex components or you may want to migrate an existing melody-component
to melody-hooks
without having to rewrite a lot. In those situations, the useReducer
hook might be the very best and most valuable tool at your disposal.
You should not rewrite Components from melody-component
to melody-hooks
unless it is absolutely needed. Both component APIs can happily co-exist and will play nicely together.
Example
A word about reducers and Redux...
The concepts used for a reducer seem very simple but there are a lot of mistakes you can make when using them.
If you intend to use them on a regular basis, you should invest some time into reading Human Redux, a book about Redux by one of its main authors.
Last updated
Was this helpful?