'How to make my reducer reusable using Redux toolkit?

How can I make my reducer be reusable in other slices?

For example, I have this reducer in my signinSlice and I want this also to be used in my signupSlice without copying and pasting it to it.

    reducers: {
        RESET_INITIAL_STATE: (state) => {
      state.status = "idle";
    },
  },

The reason why I'm asking this is I want to reduce or prevent the duplicated code all over in my other slices. By the way, I'm using redux-toolkit



Solution 1:[1]

IMHO, You should not factorize your reducer but you can just declare a function anywhere

export const resetInitialState = (state) => state.status = "idle"

and in both slices:

import { resetInitialState } from '...'

reducers: {
  RESET_INITIAL_STATE: resetInitialState
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ji aSH