redux中间件redux-thunk的作用

redux的dispatch默认只能传一个对象参数:

dispatch({ type: 'CHANGE_COLOR', themeColor: color })

redux-thunk的作用就是使dispatch支持传函数参数:

dispatch(changeColorAsyn(color))

给redux配置thunk也很简单:

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk'

const store = createStore(themeReducer, applyMiddleware(thunk))

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

App.js

import React, { Component } from 'react';
import Header from './Header'
import { Provider } from 'react-redux'
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

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, applyMiddleware(thunk))

class App extends Component {
  render() {
    return (
      <Provider store={store}>
        <Header />
      </Provider>
    );
  }
}

export default App;

Header.js

import React, { Component } from 'react';
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import ThemeSwitch from './ThemeSwitch'

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

  render () {
    return (
      <div>
        <h1 style={{ color: this.props.themeColor }}>xutongbao</h1>
        <ThemeSwitch/>
      </div>
    )
  }
}

const mapStateToProps = (state) => {
  return {
    themeColor: state.themeColor
  }
}
Header = connect(mapStateToProps, null)(Header)

export default Header

ThemeSwitch.js

import React, { Component } from 'react';
import PropTypes from 'prop-types'
import { connect } from 'react-redux'

class ThemeSwitch extends Component {
  static contextTypes = {
    store: PropTypes.object
  }

  handleSwitchColor (color) {
    if (this.props.onSwitchColor) {
      this.props.onSwitchColor(color)
    }
  }

  render () {
    return (
      <div>
        <button onClick={this.handleSwitchColor.bind(this, 'red')}>Red</button>
        <button onClick={this.handleSwitchColor.bind(this, 'blue')}>Blue</button>
        <button onClick={this.handleSwitchColorByThunk.bind(this, '#f66f0c')}>使用中间件</button>
      </div>
    )
  }
}

Object.assign(ThemeSwitch.prototype, {
  handleSwitchColorByThunk(color) {
    this.props.onSwitchColorByThunk(color)
  }
})

let changeColorAsyn = (color) => {
  return (dispatch) => {
    setTimeout(() => {
      dispatch({ type: 'CHANGE_COLOR', themeColor: color })
    }, 2000)    
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    onSwitchColor: (color) => {
      dispatch({ type: 'CHANGE_COLOR', themeColor: color })
    },
    onSwitchColorByThunk: (color) => {
      dispatch(changeColorAsyn(color))
    }
  }
}

export default connect(null, mapDispatchToProps)(ThemeSwitch)

github源代地址:https://github.com/xutongbao/redux-thunk-demo

原文地址:https://www.cnblogs.com/xutongbao/p/11915685.html