TypeError:store.getState不是函数?

时间:2017-08-04 07:49:40

标签: reactjs redux

嗨,我是新的反应在行动我想访问商店数据,以便我可以将其发送到服务器,我试图在我的行动中访问商店,就像这样  stote.getState().someKey。但是要记得上面提到的错误

我的行动:

 export default function (userInfo){
  return function (dispatch, store){
   console.log(store.getState().someKey) //TypeError: store.getState is not a function
   someFunction(userInfo).then( function (response){
    dispatch(fetched(response))
     });
  dispatch(isFetching())
  }
}

我的configureStore是这样的。

import allReducers from '../reducers';
import logger from 'redux-logger';
import {createStore, applyMiddleware} from "redux";
import {ENABLE_LOGGER} from '../config/config';
import thunk from 'redux-thunk';
import loadingBarMiddleware from './loadingbar';

function configureStore(){
 let initialState ={}
  if(ENABLE_LOGGER){
    var store = createStore(allReducers, initialState,
     applyMiddleware(thunk, logger, loadingBarMiddleware())
  );
}
 else{
   var store = createStore(allReducers,
     applyMiddleware(thunk, loadingBarMiddleware())
   );
  }
 return store;
 }
export default configureStore

1 个答案:

答案 0 :(得分:1)

问题在于你的thunk动作,第二个参数不是商店本身,而是直接函数getState。所以看起来应该像点击:

export default function (userInfo){
  return function (dispatch, getState){
    console.log(getState().someKey)
    someFunction(userInfo).then(function (response){
      dispatch(fetched(response))
    });
    dispatch(isFetching())
  }
}