Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access root state in a reducer in redux toolkit?

I have a simple counter slice. In increment function I want to access root state. How can I do that?

const initialState = {
  value: 1
}

export const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    increment: (state) => {
      state.value++
    }
  }
})
like image 784
Damon Avatar asked Oct 20 '25 03:10

Damon


1 Answers

Generally, you can't. Redux (and not only toolkit) is designed around the idea that your Reducer should only rely on it's own state value and the contents of the action coming in, nothing else.

If you really really need data from another state slice, you probably need to copy that into the action when calling it, but it is usually considered a bad idea.

You might have chosen your slice too small.

like image 57
phry Avatar answered Oct 21 '25 17:10

phry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!