为什么redux-persist在ajax调用上不能正常工作?

时间:2016-11-02 20:40:24

标签: react-native redux axios

所以我在我的行动中用axios加载数据:

import * as types from './actionTypes';
import axios from 'axios';

export function fetchQuestions(city) {
  return function (dispatch) { 
    axios.get('http://rest.learncode.academy/api/test123/tweets')
      .then((response) => {
        dispatch({type: "FETCH_QUESTIONS_SUCCESS", payload: response.data})
      })
      .catch((err) => {
        dispatch({type: "FETCH_QUESTIONS_FAILURE", payload: err})
      })
  }
};

它加载到我的reducer中的问题:

const initialState = {
    fetching: false,
    fetched: false,
    questions: [],
    error: null,
}

export default function reducer(state = initialState, action = {}) {
  switch (action.type) {
    case types.FETCH_QUESTIONS_SUCCESS:
      return {
        ...state,
        questions: action.payload
      };
    case types.FETCH_QUESTIONS_FAILURE:
      return {
        ...state,
        error: action.payload
      };
    default:
      return state;
  }
}

我在构造函数的应用程序的第一页上这样称呼它:

constructor(props){     超级(道具);     如果(!this.props.questions){       this.props.actions.fetchQuestions();     }   }

因此,我想在问题为空时才使用它。

在我console.log(questions)的另一页上,它告诉我问题是空的。虽然我之前加载了它,所以它仍然应该加载,因为我使用redux-persist。 当我点击该页面上的某个按钮并再次console.log(questions)时,它会显示数据。

0 个答案:

没有答案