useReducer
useReducer manages complex state with a reducer function.
Example
function reducer(s, a) {
switch (a.type) {
case "inc": return { n: s.n + 1 };
case "reset": return { n: 0 };
default: return s;
}
}
const [s, dispatch] = useReducer(reducer, { n: 0 });
dispatch({ type: "inc" });Exercise
Send an action.
({ type: "inc" })Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.