Reactjs 基于服务响应更新redux状态

Reactjs 基于服务响应更新redux状态,reactjs,typescript,redux,redux-thunk,immer.js,Reactjs,Typescript,Redux,Redux Thunk,Immer.js,我已经使用createSlice创建了一个actions和reducer 在其中一个操作中,我使用状态集合调用服务。我想根据服务的响应更新状态数据 成功时,我要更新状态.位置;失败时,我要更新状态.错误数据 const ManageInventoriesState = createSlice({ name: "ManageInventoriesReducer", initialState, reducers: { updateIventoryLocatio

我已经使用createSlice创建了一个actions和reducer

在其中一个操作中,我使用状态集合调用服务。我想根据服务的响应更新状态数据

成功时,我要更新
状态.位置
;失败时,我要更新
状态.错误数据

const ManageInventoriesState = createSlice({
  name: "ManageInventoriesReducer",
  initialState,
  reducers: {
    updateIventoryLocations: (state) => {
      updateService(state.oprationsList).then((resp) => {
          state.locations = resp.data;
      }).catch((err) => {
        state.errorData = err.data;
      });
    },
  },
});
但当我尝试这样做时,我得到了以下错误

无法对已撤销的代理执行“设置”

如何正确地做

从UI我只是分派操作(UI是一个应用程序)


西蒙斯,这是还原剂的反模式。并且不应该包含异步调用

此外,您的reducer需要返回更新的状态,但没有返回任何内容

您可以更新它:

减速器

updateInventoryLocations:(state,action)=>({…state,locations:action.payload})
然后调用包含服务结果的操作:

updateService(state.oprationsList)。然后((resp)=>{
调度(更新事件位置(响应数据));
});

如何使用state.oprationsList调用updateService。UI无法直接访问状态对象。为什么不能访问
OpratioList
?你能分享你调用调度的组件代码吗?我的错,看起来我可以访问列表。调查一下。谢谢再次感谢Yash,让我回到“减缩器应该是纯函数”的真理:没问题,如果它能帮助您,我将不胜感激
dispatch(updateIventoryLocations());