React · Middle · Question 9 of 75

When is useReducer preferable to useState?

Explanation

useReducer gathers update logic into one pure function (state, action) => newState: it's easy to read and test, and handlers only describe what happened (dispatch({ type: 'added' })). dispatch is stable and convenient to pass down. It doesn't make the state global.

Further reading

Problem with a question

React · Middle · question 9

Test yourself

Take the React · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz