Search code examples
reduxreducersredux-toolkit

Do actions added with extraReducers on createSlice have the slice's name prefix added to their types?


From the official doc's example:

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) => {})
  },
})

Also from the docs:

enter image description here

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.

QUESTION

In the example above, will the cases incrementBy and decrement also get the counter name as a prefix in their types?

Like:

"counter/incrementBy"
"counter/decrement"

Is this how the extraReducers property work?


Solution

  • No, because the entire point of extraReducers is that it does not generate any new action types.

    extraReducers exists so that a slice reducer can listen to other action types that have already been defined outside the slice.