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,一塊兒交流學習前端。

相關文章
相關標籤/搜索