Reputation: 2636
I've noticed, that in CreateSlice -> extraReducers, when you use builder to addCase, you can access the slice itself within the addCase method, but nowhere else!
const slice = createSlice({
name: 'sliceName',
initialState,
reducers: {...},
extraReducers: builder => {
console.log( '1: slice', slice ); // undefined
builder.addCase( someAsyncThunk.pending, ( state, action ) => {
console.log( '2: slice', slice ); // full access to slice const - HOW??
});
}
});
console.output:
1: slice undefined
2: slice {name: "sliceName", actions: {…}, caseReducers: {…}, reducer: ƒ}
how is that even possible? how can you access slice by its name but only from within the reducer passed to addCase?
Thanks in advance.
Upvotes: 0
Views: 302
Reputation: 44086
While the extraReducers
builder function gets executed at creation of the slice (at which point in time the slice does not exist yet), the case reducers themselves are executed when the slice has already been created for a long time. So they can then access the slice.
Upvotes: 2