作用:处理状态更新的逻辑,并且脱离UI,可以复用
const initialState={num:0};
const reducer=(state,action)=>{
switch(action.type)
{
case 'decre':
return {...state,num:state.num-1}
default: return state;
}
}
const ComponentReducer=()=>{
const [state,dispatch]=useReducer(reducer,initialState)
const {num}=state;
return <button onClick={()=>dispatch({type:'decre'})})</button>
}