最近在学 React
,看到 react-redux
这里,刚开始以为一脸懵逼,后面经过查阅相关资料和一些对源码的解释,总算有点头绪,今天在这里总结下。javascript
相似于 Vue
,React
中组件之间的状态管理 第三方包为:react-redux
。react-redux
实际上是 Redux
的官方React
绑定库,它可以使你的React
组件从Redux
store
中读取数据,而且向store
分发actions
以更新数据。前端
值得一提的是 redux
实际上是一个第三方 数据状态管理的库,它不单单能够和react
结合使用,你也能够把它应用到 vue
中 , react-redux
实际上是帮咱们封装了 redux
链接 react
的一些操做,使用 react-redux
能够很是简单的在 react
中使用 redux
来管理咱们应用的状态。vue
npm install redux react-redux --save
安装完这两个库以后,能够用 redux
来建立 store
, 利用 react-redux
获取 store
中的数据或者更新数据。java
react-redux
提供了两个经常使用的 api
,一个是: Provider
,一个是:connect
。 组件之间共享的数据是 Provider
这个顶层组件经过 props
传递下去的,store必须做为参数放到Provider组件中去。而 connect
则提供了组件获取 store 中数据或者更新数据的接口。react
了解一些基本的概念以后,咱们如今开始来用。git
在外围顶层组件中引入 redux
和 react-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
建立完数据以后,组件中怎样使用到全局的数据状态呢?请看下面: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 store
中 themeColor
数据, 这是咱们前面本身建立的数据,而后组件经过 this.props.themeColor
调用。
那么这样就能够实现渲染,就是把Redux中的state变成React中的props。
如今的主题颜色是本身定义的红色,若是咱们想在某个组件中修改这个全局的状态,好比修改成蓝色,该如何操做,这就涉及到修改 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
,一块儿交流学习前端。