react-native - 使用 redux 和 redux thunk 反应原生多动作
问题描述
我是反应新手,并试图为异步 api 调用实现 redux/redux-thunk。我有 5 个部分,想调用 5 个不同的 Api。我的问题是在 props 状态下我只能得到一个最后执行的 api 数据,我需要创建多个 reducer 还是需要将数据合并到一个对象中?
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
})
解决方案
推荐阅读
- c# - 如何在 Visual Studio 扩展 (VSIX) 中检测 CMake 项目加载
- json - 将 JSON 对象的输出格式化为更易读的格式
- android - 如何修复“启动屏幕上的 Android 应用程序冻结”活动?
- css - 如何使我的图像重叠在另一个图像上响应
- java - 使用 ViewModel 时导航不起作用
- nativescript-angular - 在 nativescript 下拉菜单中出现错误
- node.js - 是否可以使用机器人框架 v4 在 Microsoft-Teams 中获取 PowerBI 报告屏幕截图或报告?
- python - 如何在python中将字符串更改为向量
- amazon-web-services - AWS 从 S3 获取最近 6 个月的图像
- excel - 在名称管理器中搜索名称并设置值