React Hooks Hooks API Reference – React js
An alternative to useState. js
Accepts a reducer of type (state, action) => newState
returns the current state paired with a dispatch method.
useReducer is usually preferable to useState
- when you have complex state logic
- that involves multiple sub-values or
- when the next state depends on the previous one.
useReducer also lets you optimize performance for components
- that trigger deep updates
- because you can pass dispatch down instead of callbacks.
Here’s the counter example from the useState section, rewritten to use a reducer:
js
setState version js
React guarantees that dispatch function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the useEffect or useCallback dependency list.
Specifying the initial state There’s two different ways to initialize useReducer state. You may choose either one depending on the use case. The simplest way to pass the initial state as a second argument:
- const [[state, dispatch]] = useReducer(
- reducer,
- {count: initialCount}
- );
Note
React doesn’t use the state = initialState argument convention popularized by Redux. The initial value sometimes needs to depend on props and so is specified from the Hook call instead. If you feel strongly about this, you can call useReducer(reducer, undefined, reducer) to emulate the Redux behavior, but it’s not encouraged.
Lazy initialization You can also create the initial state lazily. To do this, you can pass an init function as the third argument. The initial state will be set to init(initialArg).
It lets you extract the logic for calculating the initial state outside the reducer. This is also handy for resetting the state later in response to an action: js
Bailing out of a dispatch If you return the same value from a Reducer Hook as the current state, React will bail out without rendering the children or firing effects. (React uses the Object.is comparison algorithm.)
Note that React may still need to render that specific component again before bailing out. That shouldn’t be a concern because React won’t unnecessarily go “deeper” into the tree. If you’re doing expensive calculations while rendering, you can optimize them with useMemo.
useReducer hook usage - Qiita Understanding React hooks from the basics (useReducer section) - Qiita
This page is auto-translated from /nishio/useReducer using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I’m very happy to spread my thought to non-Japanese readers.