我目前收到错误 TypeError: getState is not a function 我正在尝试类似于http://redux.js.org/docs/advanced/AsyncActions.html上的示例
action.js - 此处发生错误
export const fetchCategoriesIfNeeded = (dispatch, getState) => {
if(shouldFetchCategories(getState())){
return dispatch(fetchCategories())
}
}
应用程序.js
componentDidMount(){
this.props.dispatch(fetchCategoriesIfNeeded())
}
...
const mapStateToProps = (state, props) => {
return {
isFetching: state.isFetching,
categories: state.categories
}
}
减速器.js
function data (state = initialState, action){
switch(action.type){
case RECEIVE_CATEGORIES:
return {
...state,
isFetching: false,
categories: action.categories
}
case REQUEST_CATEGORIES:
return {
...state,
isFetching: true
}
default:
return state
}
return state
}
为了便于阅读,省略了一些代码。
我也试过这个并收到 TypeError: dispatch is not a function
export function fetchCategoriesIfNeeded(){
return(dispatch, getState) =>{
var state = getState()
if(shouldFetchCategories(state)){
dispatch(fetchCategories())
}
}
}