在 createSlice 上使用 extraReducers 添加的操作是否将切片的名称前缀添加到它们的类型中?
Do actions added with extraReducers on createSlice have the slice's name prefix added to their types?
来自官方文档的示例:
https://redux-toolkit.js.org/api/createSlice#the-extrareducers-builder-callback-notation
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) => {})
},
})
同样来自文档:
One of the key concepts of Redux is that each slice reducer "owns" its slice of state, and that many slice reducers can independently respond to the same action type. extraReducers allows createSlice to respond to other action types besides the types it has generated.
问题
在上面的示例中,案例 incrementBy
和 decrement
是否也会在其类型中获得 counter
名称作为前缀?
喜欢:
"counter/incrementBy"
"counter/decrement"
extraReducers
属性 是这样工作的吗?
没有。它没有获取名称前缀。
https://codesandbox.io/s/xenodochial-dew-35ivq
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;
不,因为 extraReducers
的全部意义在于它不会 生成任何新的操作类型。
extraReducers
存在以便 切片缩减器可以侦听 其他 已经在切片外定义的操作类型 .
来自官方文档的示例:
https://redux-toolkit.js.org/api/createSlice#the-extrareducers-builder-callback-notation
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) => {})
},
})
同样来自文档:
One of the key concepts of Redux is that each slice reducer "owns" its slice of state, and that many slice reducers can independently respond to the same action type. extraReducers allows createSlice to respond to other action types besides the types it has generated.
问题
在上面的示例中,案例 incrementBy
和 decrement
是否也会在其类型中获得 counter
名称作为前缀?
喜欢:
"counter/incrementBy"
"counter/decrement"
extraReducers
属性 是这样工作的吗?
没有。它没有获取名称前缀。
https://codesandbox.io/s/xenodochial-dew-35ivq
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;
不,因为 extraReducers
的全部意义在于它不会 生成任何新的操作类型。
extraReducers
存在以便 切片缩减器可以侦听 其他 已经在切片外定义的操作类型 .