0

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?

Museful
  • 6,711
  • 5
  • 42
  • 68
  • 1
    Since the reducer is always triggered by an action, keep in mind that you can access the entire state when dispatching them. If a reducer case needs read access to an outside slice, I'd just pass in that extra information in the payload. – timotgl Jun 01 '22 at 09:34

1 Answers1

2

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:

markerikson
  • 63,178
  • 10
  • 141
  • 157