3 Star 15 Fork 8

weiQwei/react-demos

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Back to Master

知识点 img.png img_1.png

  1. react-redux的基本使用

  2. 容器组件和UI组件

  3. 使用connect方法创建容器组件

    /*connect函数的参数,映射redux中的状态到UI组件的props*/
     function mapStateToProps(state) {
     /*count可以在CountUI组件的props中找到*/
     return {count: state}
     }
    
     /*connect函数的参数,映射redux中的dispatch方法到UI组件的props*/
     function mapDispatchToProps(dispatch) {
     /*increase,decrease,increaseAsync可以在CountUI组件的props中找到*/
         return {
             increase: (data) => {
                 dispatch(createIncrementAction(data))
             },
             decrease: (data) => dispatch(createDecrementAction(data)),
             increaseAsync: (data, waitTime) => dispatch(createIncrementAsyncAction(data, waitTime))
          }
     }
    
     /*使用connect函数连接UI组件和redux,并返回一个容器<组件>*/ // export default connect(mapStateToProps, mapDispatchToProps)(CountUI)
    
     // 或者简写 
     export default connect(
       (state) => ({count: state}), 
     /*这里mapDispatchToProps省略了dispatch的步骤,由react-redux内部自动dispatch*/
     { 
            increase: createIncrementAction, 
            decrease: createDecrementAction, 
            increaseAsync: createIncrementAsyncAction
     })(CountUI)
    
    
  4. Provider组件管理可以管理redux store

  5. react-redux可以自动管理store.subscribe,所以不用自己写监测

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/weiqwei/react-demos.git
git@gitee.com:weiqwei/react-demos.git
weiqwei
react-demos
react-demos
master

搜索帮助