📌  相关文章
📜  react 示例中的 reducer - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:45.158000             🧑  作者: Mango

代码示例1
const initialState = {count: 0};

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return {count: state.count + 1};
    case 'decrement':
      return {count: state.count - 1};
    default:
      throw new Error();
  }
}

function Counter() {
  const [state, dispatch] = useReducer(reducer, initialState);
  return (
    <>
      Count: {state.count}
      
      
    
  );
}