pureComponent import { useState } from "react" // useReducer, // 统一调度 function reducer(state, action) {console.log('reducer接收参数', state, action)const { type } = actionswitch (type) {case 'add':return { num: state.num + 1 }case 'minus':return { num: state.num - 1 }} } function useReducer(reducer, initState) {// initState用于初始化const [state, setState] = useState(initState)const dispatch = (action) => {const newVal = reducer(state, action)setState(newVal)}return [state, dispatch] } export default function App() {// 参数:归纳函数,初始状态const [state, dispatch] = useReducer(reducer, { num: 0 })return (<><p>数:{state.num}</p><button onClick={() => dispatch({ type: 'add' })}>+1</button><br /><button onClick={() => dispatch({ type: 'minus' })}>-1</button></>) }