useReducer
通过 dispatch
分发 action,然后由 reducer 函数根据 action 类型决定如何更新状态,适合复杂的状态更新逻辑:const reducer = (state, action) => {
switch (action.type) {
case "increment":
return { count: state.count + 1 };
case "decrement":
return { count: state.count - 1 };
default:
throw new Error();
}
};