位置:- 正文

React中使用Redux (二) - 通过react-redux库连接React和Redux(react redux reducer)

编辑:rootadmin
React中使用Redux (二) - 通过react-redux库连接React和Redux react-redux库使用Redux

推荐整理分享React中使用Redux (二) - 通过react-redux库连接React和Redux(react redux reducer),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:react reduct,react+redux,react-redux connect方法,react+redux,react react-redux,react react-redux,react-redux用法,react-redux用法,内容如对您有帮助,希望把文章链接给更多的朋友!

上一篇文章演示React中直接使用Redux的使用过程是十分繁琐的, 并且有许多重复代码

React中使用Redux (二) - 通过react-redux库连接React和Redux(react redux reducer)

但是实际上redux官方帮助我们提供了 react-redux 的库,这个库是帮助我们完成连接redux和react的辅助工具, 可以直接在项目中使用,并且实现的逻辑会更加的严谨和高效

这篇我们就可以使用react-redux库将上一篇文章的计数器案例进行优化

安装react-redux: yarn add react-redux或npm i react-redux

使用react-redux这个库分成两个步骤

步骤一: react-redux库只需要提供一次store, 我们在index.js中为App根组件提供store, 方式如下:

// index.jsimport React from 'react';import ReactDOM from 'react-dom/client';import App from './App';// 引入Provider和storeimport { Provider } from 'react-redux';import store from './store';const root = ReactDOM.createRoot(document.querySelector('#root'));root.render( // 在Provider
本文链接地址:https://www.jiuchutong.com/zhishi/300788.html 转载请保留说明!
下一篇链接:https://www.jiuchutong.com/zhishi/300789.html
免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络