(1)需求最近在学习React,在学习了ReactHook之后,做了一个useReducerDemo。(2)介绍useReducer用于方便状态管理,其用法与Redux非常相似。/**@Author:ArdenZhao*@Date:2022-04-1817:26:35*@LastEditTime:2022-04-1818:09:53*@FilePath:/react-ts/src/components/react/10-Hook-useReducer.js*@Description:文件信息*/import{useReducer}from'react';import{Button}from'antd';import"antd/dist/antd.css";//修改的方法constreducer=(state,action)=>{switch(action.type){case'add'://console.log('[state]>',state,{...state})return{...state,计数:state.count+1};case'minus':return{...state,count:state.count-1};默认值:返回状态;}}//定义对象letinitialState={count:10,name:"reducer"}//初始化方法constinit=initialState=>{return{name:"reducer",count:initialState.count+2};//初始化}functionHookUseReducer(props){//useReducerconst[state,dispatch]=useReducer(reducer,initialState,init);return(
学习,{props.name}
;计数器:{state.count}-{state.name}