前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录。最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star。


一、Redux基础介绍

  • 单向数据流:从父组件留向子组件,兄弟组件无法共享数据
  • Redux核心概念
  1. State:React中的状态,是只读对象,不可直接修改
  2. Reducer:基本函数,用于对State的业务处理
  3. Action:普通对象,用于描述事件行为,改变State
  • Redux工作流

  • Redux安装

    yarn add redux --save
    
    yarn add react-redux --save
    
  • Redux集成

  1. 创建Action模块

  2. 创建Reducer模块

  3. 创建Store模块

  4. 通过connect方法将React组件和Redux连接起来

  5. 添加<Provider />作为项目的根组件,用于数据的存储

  • Redux调试工具安装

  1. 在火狐中安装Redux Devtools扩展:【安装地址

  2. 项目中安装redux-devtools-extension调试插件

    yarn add redux-devtools-extension --save
    
  • Redux适用场景

  1. 项目需求角度考虑:

    • 用户的使用方式复杂
    • 不同身份的用户有不同的使用方式(比如普通用户和管理员)
    • 多个用户之间可以协作
    • 与服务器大量交互,或者使用了WebSocket
    • View要从多个来源获取数据
  2. 组件角度考虑:
    • 某个组件的状态,需要共享
    • 某个状态需要在任何地方都可以拿到
    • 一个组件需要改变全局状态
    • 一个组件需要改变另一个组件的状态
  3. 不适用的场景:
    • 用户的使用方式非常简单
    • 用户之间没有协作
    • 不需要与服务器大量交互,也没有使用 WebSocket
    • 视图层(View)只从单一来源获取数据

二、Redux集成

  • src->redux目录下:创建action->index.js操作事件模块

    /**
    * Action 类型:用户事件操作
    */ export const type = {
    SWITCH_MENU : 'SWITCH_MENU'
    } // 菜单点击切换,修改面包屑名称
    export function switchMenu(menuName) {
    return {
    type:type.SWITCH_MENU,
    menuName
    }
    }  
  • src->redux目录下:创建reducer->index.js数据处理模块
    /**
    * Reducer: 数据处理
    */
    import {type} from './../action' const initialState = {
    menuName: '首页'
    } const ebikeData = (state = initialState, action) => {
    switch (action.type) {
    case type.SWITCH_MENU:
    return {
    ...state, //旧值
    menuName: action.menuName //新值
    }
    break;
    default:
    return {
    ...state
    };
    }
    }
    export default ebikeData;  
  • src->redux目录下:创建store->configureStore.js数据源模块
    /**
    * 引入createStore保存数据源
    */ import {createStore} from 'redux'
    import reducer from './../reducer'
    //调试工具插件方法 -- redux降级到3.7可使用
    // import { composeWithDevTools } from 'redux-devtools-extension' export default () => createStore(reducer)  
  • src->index.js项目入口文件中:添加<Provider />项目根组件,存储store数据源

    import { Provider } from 'react-redux'
    import configureStore from './redux/store/configureStore' const store = configureStore();
    ReactDOM.render(
    <Provider store={store}>
    <Router />
    </Provider>,
    document.getElementById('root'));

三、面包屑标题切换

  

  • pages->components->NevLeft(index.js)中:菜单(Menu)点击切换改变store中的面包屑名称(MenuName)
  1. 通过connect方法将react组件与Redux连接

    export default connect()(NavLeft)  
  2. 获取this.props.dispatch方法派发事件:通过action事件自动调用reducer存入到store中
    import {connect} from 'react-redux'  //连接器
    import { switchMenu } from './../../redux/action' //事件行为 class NavLeft extends React.Component {
    state = {
    currentKey: ''
    }
    handleClick = ({item, key}) => {
    if (key === this.state.currentKey) {
    return false;
    }
    // 事件派发,自动调用reducer,通过reducer保存到store对象中
    const { dispatch } = this.props;
    dispatch(switchMenu(item.props.title))
    // console.log(item)
    this.setState({
    currentKey: key
    })
    }
    homeHandleClick = () => {
    const { dispatch } = this.props;
    dispatch(switchMenu('首页'));
    this.setState({
    currentKey: ""
    });
    }; //其它代码
    <Menu
    onClick={this.handleClick}
    selectedKeys={[this.state.currentKey]}
    theme="dark"
    >
    {this.state.MenuTreeNode}
    </Menu> }
  • pages->components->Header(index.js)中:获取并填入store中的面包屑名称(MenuName)
  1. 通过connect方法将react组件与Redux连接
  2. 利用mapStateToProps函数建立state与store的映射关系:将state.menuName 绑定到 props 的menuName
  3. 面包屑标题处:获取this.props.menuName填入
    import {connect} from 'react-redux'  //连接器
    …… //将state.menuName 绑定到 props 的menuName
    const mapStateToProps = state => {
    return {
    menuName: state.menuName
    }
    }
    export default connect(mapStateToProps)(Header) 
    关于mapStateToProps

  • mapStateToProps(state, ownProps)为一个函数,它可以传入两个参数,结果一定要返回一个 object;
  • 传入mapStateToProps之后,会订阅store的状态改变,在每次 store 的 state 发生变化的时候,都会被调用;
  • ownProps代表组件本身的props,如果写了第二个参数ownProps,那么当prop发生变化的时候,mapStateToProps也会被调用;
  • mapStateToProps可以不传,如果不传,组件不会监听store的变化,也就是说Store的更新不会引起UI的更新

——参考博客


注:项目来自慕课网

【共享单车】—— React后台管理系统开发手记:Redux集成开发的更多相关文章

  1. 【共享单车】—— React后台管理系统开发手记:主页面架构设计

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  2. 《React后台管理系统实战 :一》:目录结构、引入antd、引入路由、写login页面、使用antd的form登录组件、form前台验证、高阶函数/组件

    实战 上接,笔记:https://blog.csdn.net/u010132177/article/details/104150177 https://gitee.com/pasaulis/react ...

  3. react后台管理系统路由方案及react-router原理解析

        最近做了一个后台管理系统主体框架是基于React进行开发的,因此系统的路由管理,选用了react-router(4.3.1)插件进行路由页面的管理配置. 实现原理剖析 1.hash的方式   ...

  4. HoloLens开发手记-配置开发环境 Install the tools

    随着Build 2016开发者大会的结束,HoloLens开发包也正式开放下载.Hololens没有独立的SDK,开发特性被集成到最新的Visual Studio Update 2中.如果你没有Hol ...

  5. 【Python开发】python集成开发环境IDE搭建

    http://blog.csdn.net/pipisorry/article/details/39854707 使用的系统及软件 Ubuntu / windows Python 2.7 / pytho ...

  6. 【共享单车】—— React后台管理系统开发手记:城市管理和订单管理

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  7. 【共享单车】—— React后台管理系统开发手记:AntD Form基础组件

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  8. 【共享单车】—— React后台管理系统开发手记:Router 4.0路由实战演练

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  9. 【共享单车】—— React后台管理系统开发手记:权限设置和菜单调整(未完)

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

随机推荐

  1. Nano

    Nano命令指南 今天在输命令时,无意中输入了nano,对这个命令不太熟悉,结果不知道如何才能退出,保存,赶快查了一下资料,原来是这样的啊. 打开文件与新建文件 使用nano打开或新建文件,只需键入: ...

  2. Spring整合hibernate4:事务管理

    Spring整合hibernate4:事务管理 Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTran ...

  3. java集合类深入分析之PriorityQueue(二)

    PriorityQueue介绍 在平时的编程工作中似乎很少碰到PriorityQueue(优先队列) ,故很多人一开始看到优先队列的时候还会有点迷惑.优先队列本质上就是一个最小堆.前面一篇文章介绍了堆 ...

  4. gdb 查看变量~p长串末尾省略号, 一个页面显示不完

    # MQClientAPIImpl::getTopicRouteInfoFromNameServer () at MQClientAPIImpl.cpp: # # # # # # # # # , ar ...

  5. 获得NOTEPAD++ Download Manager的所有下载列表的内容的au3脚本

    ;~ 获得NOTEPAD++ Download Manager的所有下载列表的内容的au3脚本 ;~ 作者: 鹏程万里 ;~ Email:aprial@163.com ;~ 创建日期: 2014年11 ...

  6. Linux内核情景分析的alloc_pages

    NUMA结构的alloc_pages ==================== mm/numa.c 43 43 ==================== 43 #ifdef CONFIG_DISCON ...

  7. grep and regular expression --- ^ . *

    "^" : Start of Line anchor "." : Matches any single character except the newline ...

  8. Linux内核实践之工作队列【转】

    转自:http://blog.csdn.net/bullbat/article/details/7410563 版权声明:本文为博主原创文章,未经博主允许不得转载. 工作队列(work queue)是 ...

  9. C# split字符串

    string strSourse = "ab|||cdef"; string[] arr = strSource.Split(new string[]{"|||" ...

  10. bisect维护已排序的序列

    #!/usr/bin/env python # -*- coding:utf-8 -*- # author:love_cat import bisect # 用来处理已经排序好的序列,升序 # 二分查 ...