I get the following error when I add a specific action in the extraReducers
inside a slice:
Uncaught TypeError: Cannot read properties of undefined (reading 'type')
.
Example:
import { createSlice } from '@reduxjs/toolkit'
export const mySlice = createSlice({
name: 'name',
initialState,
extraReducers: (builder) => {
// If I console.log action2 here, it turns out to be undefined
builder.addCase(action1, () => ...)
builder.addCase(action2, () => ...) // when I add this specific action I get the error.
},
reducers: {
...
},
})
action2
is defined like action1
, in another file:
import { createAction } from '@reduxjs/toolkit'
export const action2 = createAction(
'action2'
)
Why action2
is undefined in mySlice
? Did I miss something?
Update: from the documentation:
Action creators that were generated using createAction may be used directly as the keys here, using computed property syntax.
If I got the sentence properly, I can replace this part:
builder.addCase(action2, () => ...)
with this:
builder.addCase("action2", () => ...)
That indeed would resolve the problem. Would this solution be correct?
Even so, it could be nice to understand why action2
appears to be undefined
when used with the first approach.