如何在 Redux Toolkit 中使用带有切片的 Redux Promise 中间件?

How to use Redux Promise Middleware with slices in Redux Toolkit?

我正在使用 Redux Toolkit 的 slices 功能。如您所知,每创建一个 reducer.

一个切片 returns 一个 action

我想使用 redux-promise-middleware 库,它针对给定的 ACTION_TYPE 创建三个可能的新操作:ACTION_TYPE_FETCHINGACTION_TYPE_FULFILLEDACTION_TYPE_REJECTED.我如何从 slices 的角度处理这个问题?

您需要将预期的操作类型添加到 createSliceextraReducers 部分,例如:

// could use a predefined action constant if desired:
const actionFetching = "ACTION_TYPE_FETCHING"

const usersSlice = createSlice({
  name: "users",
  initialState,
  reducers: {
    // specific case reducers here
  },
  extraReducers: {
    // could use computed key syntax with a separate type constant
    [actionFetching ]: (state, action) => {}
    // or the actual field name directly matching the type string
    ACTION_TYPE_FULFILLED: (state, action) => {}
  }
})