import React ,{useReducer}from 'react'; import './App.css'; const App =() =>{ const reduce =(state,action)=>{ const actionFn = { add:function(){ return { ...state, count:state.count+1 } } } return actionFn[action.type](); } const [state,dispatch] = useReducer(reduce,{count:0}) const addCount = () =>{ dispatch({type:'add'}) } return ( <div> <div>{state.count}</div> <button onClick={addCount}>+1</button> </div> ); } export default App;
标签:基本,count,const,reduce,react,state,return From: https://www.cnblogs.com/malong1992/p/17455635.html