【发布时间】:2018-08-16 09:47:54
【问题描述】:
我正在尝试从 api 获取 json 响应并成功获取数据,但是当我使用 redux-thunk 在另一个操作中调用一个操作时,我的数据在减速器中不可用。 我需要减速器内“数据”属性中的数据进入组件。检查下面的代码。
这是我的行动
import { GET_REPORT, GET_DATA_SUCCESS } from './types';
export const getData = (text) => {
return (dispatch) => {
dispatch ({ type: GET_REPORT})
fetch('http://api.openweathermap.org/data/2.5/forecast?q='+text+'&cnt=1&units=metric&APPID={key}')
.then(response => response.json())
.then(data => getDataSuccess(dispatch, data.list[0]))
.catch((error) => console.log(error));
};
};
const getDataSuccess = (dispatch, data) => {
//console.log(data);
dispatch({
type: GET_DATA_SUCCESS,
payload: data
});
}
这是我的减速器
import { GET_REPORT } from'../actions/types';
const INITIAL_STATE = {
data: '',
}
export default (state = INITIAL_STATE, action) => {
switch(action.type){
case GET_REPORT:
console.log(action.payload); // getting undefined
return {...state, data: action.payload};
default:
return state;
}
}
我需要“数据”属性中的数据进入组件。
【问题讨论】:
标签: react-native redux react-redux redux-thunk