React native 使用redux和redux thunk响应本机多个操作
我是react新手,正在尝试为异步api调用实现redux/redux thunk。 我有5个部分,想调用5个不同的Api。我的问题是,在props状态下,我只能获得一个最后执行的api数据,我需要创建多个简化程序还是需要将数据合并到一个对象中React native 使用redux和redux thunk响应本机多个操作,react-native,redux,React Native,Redux,我是react新手,正在尝试为异步api调用实现redux/redux thunk。 我有5个部分,想调用5个不同的Api。我的问题是,在props状态下,我只能获得一个最后执行的api数据,我需要创建多个简化程序还是需要将数据合并到一个对象中 const mapStateToProps = (state) => ({ isLoading: state.serviceReducer.isLoading, error: state.serviceReducer.error,
const mapStateToProps = (state) => ({
isLoading: state.serviceReducer.isLoading,
error: state.serviceReducer.error,
data: state.serviceReducer.data,
UserMaster: state.UserMasterReducer
});
function mapDispatchToProps(dispatch) {
return {
updateUserMaster: (data) => dispatch({ type: 'UPDATE_USER_MASTER', payload: data }),
ApiService1: (url,payload,id) => dispatch(ApiService(url,payload,id)),
ApiService2: (url,payload,id) => dispatch(ApiService(url,payload,id)),
}
}
减速器
import * as Actions from '../action/ActionTypes'
const ServiceReducer = (state = { isLoading: false, error: undefined, data: {}, ActionId: undefined }, action) => {
switch (action.type) {
case Actions.SERVICE_PENDING:
return Object.assign({}, state, {
isLoading: true,
ActionId:action.id
});
case Actions.SERVICE_ERROR:
return Object.assign({}, state, {
isLoading: false,
error: action.error,
ActionId:action.id
});
case Actions.SERVICE_SUCCESS:
return Object.assign({}, state, {
isLoading: false,
data: action.data,
ActionId:action.id
});
default:
return state;
}
}
export default ServiceReducer;
行动
export const serviceActionPending = (id) => ({
type: ActionTypes.SERVICE_PENDING,
id:id
})
export const serviceActionError = (error,id) => ({
type: ActionTypes.SERVICE_ERROR,
error: error,
id:id
})
export const serviceActionSuccess = (data,id) => ({
type: ActionTypes.SERVICE_SUCCESS,
data: data,
id:id
})
你说我的问题是什么意思?在props状态下,我只能得到一个最后执行的api数据???@他说的是mapStateToProps的数据:state.serviceReducer.data。从componentdidmount,我可以调用5个API,但在哪里处理数据