惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - 风焰庄主

React中,useContext+useReducer对比Redux python, C++, C# 计算速度简单对比 对三层架构的简单改进 .net程序在64位系统上报 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 问题解决 DropBox把目录设置在U盘里面的方法 工作过公司的体会--结构完整的企业其实还是挺吸引人的 努力与付出 微软用于写Javascript的一个插件,Script# 项目管理的一点想法 介绍一况挺好用的Javascript编辑器(带项目) 简单的C#进行图片操作 PHP访问C#建立的Webservice 搜索引擎一:介绍 纪念一下Jerry 在中信66楼微软Offic参加讲座 关于地理信息信息点数据采集一些方法 介绍一下JS调试和浏览器调试工具 服务器不支持WebResource.axd的特殊处理 嫦娥一号发射
React中,useReducer和useState
风焰庄主 · 2021-01-22 · via 博客园 - 风焰庄主

useReducer和redux

首先,useReducer 和使用 redux 十分类似。但是useReducer不是一个整合的store,redux是。

userReducer中的dispatch是各自独立的,不像redux,是共同的。

useReducer和useState

如果你的state被多个component引用,请使用useReducer。

useState和useReducer的关系

 1 let memoizedState;    
 2 function useReducer(reducer, initialArg, init) {    
 3   let initState = void 0;    
 4   if (typeof init !== 'undefined') {    
 5     initState = init(initialArg)    
 6   } else {    
 7     initState = initialArg    
 8   }    
 9   function dispatch(action) {    
10     memoizedState = reducer(memoizedState, action)    
11     render()    
12   }    
13   memoizedState = memoizedState || initState    
14   return [memoizedState, dispatch]    
15 }    
16     
17 function useState(initState) {    
18   return useReducer((oldState, newState) => newState, initState)    
19 }