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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - 风焰庄主

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 }