React,Redux。为什么我的派遣调度功能体?

时间:2017-08-18 14:38:06

标签: reactjs redux redux-thunk

以下是我的商店创建,我在redux-thunk功能

中应用了applyMiddleware
import { createStore, combineReducers, applyMiddleware } from 'redux'
// import { composeWithDevTools } from 'remote-redux-devtools'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import promise from 'redux-promise-middleware'
import tokenReducer from './reducers/tokenReducer'

const rootReducer = combineReducers({
  tokenReducer
})

const middleware = applyMiddleware(logger, promise, thunk)

let store = createStore(rootReducer, middleware)

export default store

然后我将我的组件连接到商店和mapDispatchToProps

function mapDispatchToProps (dispatch) {
  return {
    getToken: () => dispatch(fetchTokenFromStorage())
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(SplashScreen)

这是我的函数代码fetchTokenFromStorage

export function fetchTokenFromStorage () {
  return (dispatch) => {
    dispatch(getToken())
    AsyncStorage.getItem('AuthToken')
      .then(function (token) {
        dispatch(getTokenSuccess(token))
      })
      .catch(function () {
        dispatch(getTokenFailure())
      })
  }
}

执行this.props.getToken()后 在我的控制台中,操作如下所示:

  行动ƒ(派遣){
      调度(为gettoken());
      _reactNative.AsyncStorage.getItem('AuthToken')。then(function(token){
        调度(getTokenSuccess(令牌));
      })。catch(function(){
        派遣(得到......

这是我的函数fetchTokenFromStorage代码。 React将我的函数转换为操作类型名称。为什么呢?

1 个答案:

答案 0 :(得分:1)

thunk必须是applyMiddleware

中的第一个

const middleware = applyMiddleware(thunk, logger, promise)