当前位置:网站首页>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
边栏推荐
- Processbuilder tool class
- [point cloud series] foldingnet: point cloud auto encoder via deep grid deformation
- Uninstall MySQL database
- POM of SSM integration xml
- Playwright contrôle l'ouverture de la navigation Google locale et télécharge des fichiers
- 普通大学生如何拿到大厂offer?敖丙教你一招致胜!
- [point cloud series] so net: self organizing network for point cloud analysis
- 【快排】215. 数组中的第K个最大元素
- [walking notes]
- [point cloud series] unsupervised multi task feature learning on point clouds
猜你喜欢

超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!

交叉碳市场和 Web3 以实现再生变革

面试官给我挖坑:URI中的 “//” 有什么用?

“湘见”技术沙龙 | 程序员&CSDN的进阶之路
![[point cloud series] relationship based point cloud completion](/img/87/153a78a229a2f2d38c7b2b5ef0fb6a.png)
[point cloud series] relationship based point cloud completion

Short name of common UI control

You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!

LeetCode_DFS_中等_695.岛屿的最大面积
![[point cloud series] so net: self organizing network for point cloud analysis](/img/3c/6136b7aa322c42089f40ce13a2d747.png)
[point cloud series] so net: self organizing network for point cloud analysis

@优秀的你!CSDN高校俱乐部主席招募!
随机推荐
What do the raddr and rport in webrtc ice candidate mean?
Is Hongmeng system plagiarism? Or the future? Professional explanation that can be understood after listening in 3 minutes
集简云 x 飞书深诺,助力企业运营部实现自动化办公
切线空间(tangent space)
Armv8m (cortex M33) MPU actual combat
[point cloud series] Introduction to scene recognition
QT调用外部程序
Imx6ull QEMU bare metal tutorial 1: GPIO, iomux, I2C
kettle庖丁解牛第16篇之输入组件周边讲解
解决虚拟机中Oracle每次要设置ip的问题
解决tp6下载报错Could not find package topthink/think with stability stable.
Example of specific method for TIA to trigger interrupt ob40 based on high-speed counter to realize fixed-point machining action
Part 3: docker installing MySQL container (custom port)
Playwright controls local Google browsing to open and download files
[wechat applet] flex layout usage record
爱可可AI前沿推介 (4.23)
为什么从事云原生开发需要学习容器技术
[point cloud series] learning representations and generative models for 3D point clouds
Example interview | sun Guanghao: College Club grows and starts a business with me
【行走的笔记】