未处理的拒绝(错误):减速器执行时,您不得调用store.getState()。

时间:2018-11-28 14:25:28

标签: redux redux-thunk

未处理的拒绝(错误):减速器执行时,您不得调用store.getState()。减速器已经接收到该状态作为参数。从顶部减速器将其传递下来,而不是从商店中读取。

我的母语不是英语。因此,我会尽力清楚地表达问题! 我想通过redux-thunk异步获取一些数据。 我是一个初学者,真的解决不了这个问题。

Action.js

export const initStateAction = (data) => ({
    type: INIT_LIST_ACTION,
    data
})

export const getListAction = () => {
    return (dispatch) => {
        axios.get("/data.json").then((res) => {
            const data =res.data;
            console.log(data);
            const action = initStateAction(data);
            dispatch(action); 
       })
    }
}

reducers.js

export default ( state = defaultState, action) => {
    switch (action.type) {
      case INIT_LIST_ACTION: {
        let newState = JSON.parse(JSON.stringify(state));
        newState.list = action.data;
        return newState;
      }
    }
}
    

store.js

const composeEnhancers =
  typeof window === 'object' &&
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?   
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
    }) : compose;

const enhancer = composeEnhancers(
  applyMiddleware(thunk),
);
const store = createStore(reducer, enhancer)

export default store;

组件

    componentDidMount () {
        const action = getListAction();
        store.dispatch(action);
        this.setState(store.getState());
   
    }

看照片 enter image description here

0 个答案:

没有答案