useReducerAtom is a utility hook that implements a Redux-style reducer pattern with Jotai atoms.
When to use it
This hook was designed for complex state updates using the reducer pattern. However, it’s recommended to create your own implementation using the recipe pattern instead.Signature
Parameters
anAtom: A primitive atom to manage with a reducerreducer: A function that takes the current value and an action, returning the new valueoptions: Optional configuration objectstore: Custom store to use (defaults to the store from Provider)
Returns
A tuple containing:- The current state value
- A dispatch function to send actions
Basic Usage (Deprecated)
Recommended Alternative: Recipe Pattern
Instead of usinguseReducerAtom, create a custom atom with built-in actions:
Alternative: Action Atoms
For even better separation of concerns, create individual action atoms:Complex State Example (Recipe Pattern)
Migration Guide
If you’re usinguseReducerAtom, migrate by:
- Create a writable atom that handles actions in its write function
- Replace
useReducerAtomwithuseAtom - The API remains the same:
[state, dispatch]