当前位置:网站首页>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
边栏推荐
- MySQL 8.0.11 download, install and connect tutorials using visualization tools
- NPM err code 500 solution
- 解决虚拟机中Oracle每次要设置ip的问题
- Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
- The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
- Common commands of ADB shell
- Analysis of the latest Android high frequency interview questions in 2020 (BAT TMD JD Xiaomi)
- kettle庖丁解牛第16篇之输入组件周边讲解
- Solve the problem that Oracle needs to set IP every time in the virtual machine
- Servlet of three web components
猜你喜欢
The interviewer dug a hole for me: what's the use of "/ /" in URI?
The difference between string and character array in C language
超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
CSDN高校俱乐部“名师高校行”——湖南师范大学站
TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
Imx6ull QEMU bare metal tutorial 1: GPIO, iomux, I2C
CSDN College Club "famous teacher college trip" -- Hunan Normal University Station
EMMC / SD learning notes
On the bug of JS regular test method
Solve the problem of Oracle Chinese garbled code
随机推荐
[barycentric coordinate interpolation, perspective correction interpolation] principle and usage opinions
Feature Engineering of interview summary
【官宣】长沙软件人才实训基地成立!
Filter and listener of three web components
【视频】线性回归中的贝叶斯推断与R语言预测工人工资数据|数据分享
[wechat applet] flex layout usage record
SPI NAND flash summary
[point cloud series] summary of papers related to implicit expression of point cloud
XML
5 tricky activity life cycle interview questions. After learning, go and hang the interviewer!
MySQL5.5安装教程
[notes de marche]
X509 parsing
面试官给我挖坑:URI中的 “//” 有什么用?
[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation
[quick platoon] 215 The kth largest element in the array
Is Hongmeng system plagiarism? Or the future? Professional explanation that can be understood after listening in 3 minutes
Scons build embedded ARM compiler
Solve the problem that Oracle needs to set IP every time in the virtual machine
Android clear app cache