微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从thunkAction调度另一个thunkAction

如何解决从thunkAction调度另一个thunkAction

我在React App中使用redux-toolkit,我有这样的情况:

  1. 从deleteEntites thunkAction发送API调用删除一些 数据库中的实体
  2. 发送getAllEntities thunkAction或发送另一个API调用获取Fresh实体 清单?

问题是在deleteEntities API调用成功后我是否应该调度getAllEntities thunkAction?(如果是,怎么办?)

OR

我应该像在getAllthunkAction.fullfilled中一样发送getAllEntities API调用并在deletethunkAction.fullfilled reducer中重复操作吗?

    //queryParams are no longer needed
    export const getDevices = createAsyncThunk('devices/getDevices',async (thunkAPI:any) => {       // have to find the datatype of thunkAPI
        try {
            const response=await requestFromServer.getAllDevices();
            return response.data;
        } catch (err) {
            // Use `err.response.data` as `action.payload` for a `rejected` action,// by explicitly returning it using the `rejectWithValue()` utility
            return thunkAPI.rejectWithValue(JSON.stringify(err.response.data))
        }  
    })

    // Delete one/many devices with ids and then from entities list
    export const deleteDevices = createAsyncThunk('devices/deleteDevices',async (ids:any,thunkAPI:any) => {       // have to find the datatype of thunkAPI
        try {
          const deleteResponse = await requestFromServer.deleteDevices(ids);
          // Is this OK or I should dispatch getDevices thunk here? a bad practice I guess? 
          const getResponse = await requestFromServer.getAllDevices();
          return getResponse.data;
        } catch (err) {
          // Use `err.response.data` as `action.payload` for a `rejected` action,// by explicitly returning it using the `rejectWithValue()` utility
          return thunkAPI.rejectWithValue(JSON.stringify(err.response.data))
        }
    })

然后在createSlice的extraReducer中

/// The same reducer logic will be copied to devicesThunks.deleteDevices.fulfilled
.addCase(devicesThunks.getDevices.fulfilled,(state,action) => {
        const entities = action.payload;
        .
        .
        .
        return devicesAdapter.setAll({
          ...state,listLoading: false,.
          .
        },entities);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?