How does useReducer work?

React hooks and effects · Freshers and experienced

Model answer

const [state, dispatch] = useReducer(reducer, initialState). The reducer is a pure function (state, action) => newState. You call dispatch({ type: 'add', payload }) and React runs the reducer and re-renders with the result.

It centralises update rules, which is clearer than many setState calls when the next state depends on the action type.

Learn this topic

React useEffect Explained: Side Effects, Dependencies and Cleanup in the course React.js Full Course: Beginner to Advanced.

Sign in to add your own answer.

More questions