Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Redux 在createSlice上添加了ExtraReducer的操作是否具有切片';s的名称前缀添加到其类型中?_Redux_Reducers_Redux Toolkit - Fatal编程技术网

Redux 在createSlice上添加了ExtraReducer的操作是否具有切片';s的名称前缀添加到其类型中?

Redux 在createSlice上添加了ExtraReducer的操作是否具有切片';s的名称前缀添加到其类型中?,redux,reducers,redux-toolkit,Redux,Reducers,Redux Toolkit,根据官方文件的示例: 这就是extraReducers属性的工作方式吗?否。它没有名称前缀 不,它没有名称前缀 不,因为extraReducers的全部要点是它不会生成任何新的动作类型 extraReducers的存在使得切片缩减器可以侦听已在切片外部定义的其他动作类型否,因为extraReducers的全部要点是它不会生成任何新的动作类型 ExtraReducer的存在使得切片缩减器可以侦听已在切片外部定义的其他操作类型ExtraReducer的要点是对从另一个源发送的操作作出反应。

根据官方文件的示例:


这就是
extraReducers
属性的工作方式吗?

否。它没有名称前缀


不,它没有名称前缀


不,因为
extraReducers
的全部要点是它不会生成任何新的动作类型


extraReducers
的存在使得切片缩减器可以侦听已在切片外部定义的其他动作类型

否,因为
extraReducers
的全部要点是它不会生成任何新的动作类型


ExtraReducer
的存在使得切片缩减器可以侦听已在切片外部定义的其他操作类型

ExtraReducer的要点是对从另一个源发送的操作作出反应。由于“另一个源”意味着没有从它们生成动作创建者,因此切片具有“无控制权”,因此无法向动作类型添加任何内容。
extraReducers
的要点是对从另一个源发送的动作作出反应。由于“另一个源”意味着没有从它们生成动作创建者,因此切片具有“无控制”,因此不可能向动作类型添加任何内容。
import { createAction, createSlice } from '@reduxjs/toolkit'
const incrementBy = createAction<number>('incrementBy')
const decrement = createAction('decrement')

createSlice({
  name: 'counter',
  initialState: 0,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(incrementBy, (state, action) => { // DO SOMETHING })
      .addCase(decrement,   (state, action) => { // DO SOMETHING })
      .addDefaultCase((state, action) => {})
  },
})
"counter/incrementBy"
"counter/decrement"
import { createAction, createSlice } from "@reduxjs/toolkit";

interface CounterState {
  value: number;
}

export const decrementV2 = createAction('decrement');

const initialState = { value: 0 } as CounterState;

const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    increment(state,action) {
      console.log(`action.type: ${action.type}`);
      state.value++;
    },
    decrement(state,action) {
      console.log(`action.type: ${action.type}`);
      state.value--;
    }
  },
  extraReducers: (builder) => {
    builder.addCase(decrementV2, (state, action) => {
      console.log("FROM decrementV2 (from extraReducers)")
      console.log(`action.type: ${action.type}`);
      state.value--;
    });
  }
});

export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;