【问题标题】:Is it possible to create a slice in redux-toolkit using only extraReducers and an empty reducers object?是否可以仅使用 extraReducers 和一个空的 reducers 对象在 redux-toolkit 中创建切片?
【发布时间】:2021-11-22 14:50:00
【问题描述】:

我有以下代码:

export const walkSlice = createSlice({
    name: "walk"
  , initialState
  , reducers: {}
  ,extraReducers: (builder) => {
    builder
      // Change status to loading when API call is pending
      .addCase(fetchWalk.pending, (state) => {
        state.status = cStatusType.Loading;
      })
...

当 reducers 设置为空对象时,切片似乎不会出现在 Redux 状态。我只想使用extraReducers 来处理API 调用。我已经测试过添加一个空的减速器,例如。

reducers: {
    add: (state) => {
      
    },
...

这似乎将切片添加到状态。有没有解决的办法?如果可能,我不想要不必要的代码。

【问题讨论】:

    标签: reactjs redux react-redux redux-toolkit


    【解决方案1】:

    您可以将createSlice 与一个空的reducers 对象一起使用,是的,但没有真正的意义。 createSlice 的目的 reducers 对象内部定义case reducer,然后生成相应的动作类型和动作创建者。如果您收听已定义的其他操作类型并且使用extraReducers字段,则应改用createReducer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-10
      • 2020-12-13
      • 2021-06-06
      • 2013-06-13
      • 2023-02-02
      • 2020-10-08
      • 2021-08-17
      • 1970-01-01
      相关资源
      最近更新 更多