当前位置:网站首页>Usereducer basic usage
Usereducer basic usage
2022-04-23 13:38:00 【Nianya】
One 、 introduce api
import React ,{useReducer}from 'react';
Two 、 Basic use
Set the value count, The default initial value is 0,
Pass in different distribution methods ,‘add’,‘sub’,
according to action Different , Return different values .
function UseReducerDemo() {
const [count, dispatch]=useReducer((state,action)=>{
switch (action) {
case 'add':
return state+1;
case 'sub':
return state-1;
default:
return state;
}
},0)
return<div>
<h2> Now the score is :{count} branch </h2>
<button style={
{marginRight:10}} onClick={()=>{dispatch('add')}}> Bonus points </button>
<button onClick={()=>{dispatch('sub')}}> points </button>
</div>
}
export default UseReducerDemo
3、 ... and 、 design sketch

End of example .
Four 、 Other related usage
The second parameter receives the value , Also accept {} object ,eg:
const initialState = {count: 0};
const [state, dispatch] = useReducer(reducer, initialState);
It can also be used in this way
function init(initialCount) {
return {count: initialCount};
}
function reducer(state, action) {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
case 'reset':
return init(action.payload);
default:
throw new Error();
}
}
function Counter({initialCount}) {
const [state, dispatch] = useReducer(reducer, initialCount, init);
return (
<>
Count: {state.count}
<button
onClick={() => dispatch({type: 'reset', payload: initialCount})}>
Reset
</button>
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
<button onClick={() => dispatch({type: 'increment'})}>+</button>
</>
);
版权声明
本文为[Nianya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230605115779.html
边栏推荐
猜你喜欢

web三大组件之Servlet

榜样专访 | 孙光浩:高校俱乐部伴我成长并创业

SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置

浅谈js正则之test方法bug篇

MySQL5. 5 installation tutorial
![[dynamic programming] 221 Largest Square](/img/31/3af4ba05764a683220b5292d3b8780.png)
[dynamic programming] 221 Largest Square

SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
![[Journal Conference Series] IEEE series template download guide](/img/1b/a3cf2688778ec34354e77e28e32be8.png)
[Journal Conference Series] IEEE series template download guide

数据仓库—什么是OLAP

X509 parsing
随机推荐
Plato farm, a top-level metauniverse game, has made frequent positive moves recently
The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
Machine learning -- naive Bayes
According to the salary statistics of programmers in June 2021, the average salary is 15052 yuan. Are you holding back?
X509 parsing
零拷贝技术
Example of specific method for TIA to trigger interrupt ob40 based on high-speed counter to realize fixed-point machining action
十万大学生都已成为猿粉,你还在等什么?
MySQL 8.0.11 download, install and connect tutorials using visualization tools
数据仓库—什么是OLAP
TCP 复位gongji原理和实战复现
超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
TIA博途中基於高速計數器觸發中斷OB40實現定點加工動作的具體方法示例
UEFI learning 01-arm aarch64 compilation, armplatformpripeicore (SEC)
【快排】215. 数组中的第K个最大元素
普通大学生如何拿到大厂offer?敖丙教你一招致胜!
[point cloud series] relationship based point cloud completion
SSM整合之pom.xml
@Excellent you! CSDN College Club President Recruitment!
You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!