Search code examples
javascriptreduxreact-reduxredux-reducers

Possible to add a reducer that can read from multiple state slices?


I am using createSlice to create slices of state. Let's call them sliceA and sliceB. Mostly these slices are decoupled from each other. However I would like to create one reducer in sliceA that has (read) access to both sliceA and sliceB. Is this possible?

I know I can use extraReducers to accept actions that were not defined in the slice. But I think I would still be limited (in these extra reducers) to read only sliceA, and not sliceB.

Any idea how I could create a reducer that can read from both slices?


Solution

  • By default, you can't. Each slice reducer is a standalone encapsulated function that can only see its own state.

    Per the Redux FAQ entry on "How can I share state across slices?", your main options are: