ReactQuestion 05 of 28
What is useState and useReducer?
Answer out loud or in your head first, then compare.
Answer
useState:
JavaScript
const [count, setCount] =
useState(0);good for simple state.
useReducer:
JavaScript
const [state, dispatch] =
useReducer(reducer, initialState);We describe changes as actions:
JavaScript
dispatch({
type: "ADD_ITEM",
payload: item
});useReducer is a good fit when:
- the state is complex,
- there are many kinds of changes,
- many fields depend on each other,
- you want to describe the update logic in one place.
How did it go? Be honest — “Not yet” puts the question on your review list.