当前位置: 首页 > Web前端 > JavaScript

Day42-100ReactHook中useReducer的基本用法

时间:2023-03-26 21:50:36 JavaScript

(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}

dispatch({type:'add'})}>plus+dispatch({type:'minus'})}>minus-
);}exportdefaultHookUseReducer写在学习路上的最后一句话,经常懈怠《有想学技术需要监督的同学嘛~》https://mp.weixin.qq.com/s/Fy...如果有需要的小伙伴可以加我微信:learningisconnecting或者关注公众号:国星聊天成长(我会分享生长方式)