我想用axios加載數據,但無法將數據提取到狀態。異步操作返回數組[0]
我減速機:
import * as types from '../actions/actionTypes';
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;
}
}
我的行動的創建者:
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) => {
console.log("Test:" + response.data) //This returns [Object Object]
dispatch({type: "FETCH_QUESTIONS_SUCCESS", payload: response.data})
})
.catch((err) => {
dispatch({type: "FETCH_QUESTIONS_FAILURE", payload: err})
})
}
};
中的console.log在那裏確實給我[對象對象。但是,當我打電話的動作不會放任何東西的狀態questions
const {questions, actions} = this.props;
const openQuestionOverview = (test) => {
actions.fetchQuestions();
console.log(questions); //Returns Array[0] for questions
}
return(
<TouchableHighlight onPress={openQuestionOverview}>
<Image source={button} />
</TouchableHighlight>
)
'types.FETCH_QUESTIONS_SUCCESS'的值是什麼? – QoP