react-redux 的使用

前言

最近在学 React,看到 react-redux 这里,刚开始以为一脸懵逼,后面经过查阅相关资料和一些对源码的解释,总算有点头绪,今天在这里总结下。javascript

相似于 VueReact 中组件之间的状态管理 第三方包为:react-reduxreact-redux 实际上是 Redux的官方React绑定库,它可以使你的React组件从Redux store中读取数据,而且向store分发actions以更新数据。前端

值得一提的是 redux 实际上是一个第三方 数据状态管理的库,它不单单能够和react 结合使用,你也能够把它应用到 vue 中 , react-redux 实际上是帮咱们封装了 redux 链接 react 的一些操做,使用 react-redux 能够很是简单的在 react 中使用 redux 来管理咱们应用的状态。vue

使用 redux 来管理 react 数据

开始以前先安装

npm install redux react-redux --save

安装完这两个库以后,能够用 redux 来建立 store , 利用 react-redux 获取 store 中的数据或者更新数据。java

react-redux 提供了两个经常使用的 api ,一个是: Provider,一个是:connect。 组件之间共享的数据是 Provider 这个顶层组件经过 props 传递下去的,store必须做为参数放到Provider组件中去。而 connect 则提供了组件获取 store 中数据或者更新数据的接口。react

建立 store

了解一些基本的概念以后,咱们如今开始来用。git

在外围顶层组件中引入 reduxreact-redux 两个库。咱们在作业务以前都须要将页面拆分红不一样的组件,这里的外围组件一般指的是咱们拆分后的全部组件的父组件。github

import { createStore } from 'redux'
import { Provider } from 'react-redux'

引入 createStore 来建立组件共享的数据,这个是 redux 中提供的一个方法,咱们直接引入。web

const themeReducer = (state, action) => {
  if (!state) return {
    themeColor: 'red'
  }
  switch (action.type) {
    case 'CHANGE_COLOR':
      return { ...state, themeColor: action.themeColor }
    default:
      return state
  }
}

const store = createStore(themeReducer)

上面的代码建立了一个 {themeColor: 'red'}store,而且提供了修改颜色的方法,组件经过指定的 action.type 中的 CHANGE_COLOR 字段来修改主体颜色。代码中能够看出,咱们传入非法的修改字段名,则返回原始的 state,即修改失败。shell

使用 store 中的 state

建立完数据以后,组件中怎样使用到全局的数据状态呢?请看下面:npm

在须要使用数据的组件中引入 react-redux

import { connect } from './react-redux'

咱们从 react-redux 中引入了 connect 这个方法。其实 connect 方法一共有4个参数,这里主要讲前两个。

connect(mapStateToProps, mapDispatchToProps)(MyComponent)

mapStateToProps 字面含义是把state映射到props中去,意思就是把Redux中的数据映射到React中的props中去。

也就是说你React想把Redux中的哪些数据拿过来用。

class Header extends Component {
  static propTypes = {
    themeColor: PropTypes.string
  }

  render () {
    return (
      <h1 style={{ color: this.props.themeColor }}>React.js 小书</h1>
    )
  }
}

const mapStateToProps = (state) => {
  return {
    themeColor: state.themeColor
  }
}

Header = connect(mapStateToProps)(Header)

上面代码是拿到 Redux storethemeColor 数据, 这是咱们前面本身建立的数据,而后组件经过 this.props.themeColor 调用。

那么这样就能够实现渲染,就是把Redux中的state变成React中的props。

修改 store 中 state

如今的主题颜色是本身定义的红色,若是咱们想在某个组件中修改这个全局的状态,好比修改成蓝色,该如何操做,这就涉及到修改 store 中 state。

修改 Redux 中的 state ,须要用到前面 connect 中的第二个参数:mapDispatchToProps,经过上面的分析,相信这个函数也很好理解,就是把各类 dispatch也变成了 props 让你能够直接使用,进而修改 store 中的数据。

class SwitchColor extends Component {
  handleChangeColor (color) {
    this.props.changeColor(color)
  }
  render() {
    return (
      <div>
        <button style={{color: this.props.themeColor}} onClick={this.handleChangeColor.bind(this, 'blue')}>blue</button>
        <button style={{color: this.props.themeColor}} onClick={this.handleChangeColor.bind(this, 'red')}>red</button>
      </div>
    )
  }
}
const mapStateToProps = (state) => {
  return {
    themeColor: state.themeColor
  }
}
const mapDispatchToProps = (dispatch) => {
  return {
    changeColor: (color) => {
      dispatch({type: 'CHANGE_COLOR', themeColor: color})
    }
  }
}
SwitchColor = connect(mapStateToProps, mapDispatchToProps)(SwitchColor)

上面的代码实现了经过点击按钮来修改主题颜色,咱们在 mapDispatchToProps 中调用了 dispatch() 来通知 Redux store 修改 数据,这里须要注意传入 dispatch() 的参数为一对象,其中必须有 type 属性来告诉 store 修改哪些数据。

说明

本篇文章 出自于 咱们 GitHub 仓库 web-study ,详情可见:戳这里, 欢迎 star,一块儿交流学习前端。

相关文章
相关标签/搜索