Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

使用useReducer 和 useContext,非首次dispatch会触发两次reducer,这是为什么?

这个项目的目的是想实现使用useReducer 和 useContext 管理全局状态和跨组件传递数据, 类似Redux;

但是,对于非首次触发的dispatch,每次action都会触发两次reducer,这是为什呢?

reducers.ts 代码如下:
image.png

context.tsx代码如下:
image.png

用法如下:
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

可能是因为添加了React.StrictMode,删除后就正常了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...