In Redux tool kit there is an option to set initial state. So we can create a reducer to set initial state.
My problem is, will it update all of the components which use that state?
import { createSlice } from '@reduxjs/toolkit'
const counterSlice = createSlice({
name: 'counter',
initialState: {name: 'stack'},
reducers: {
reset: () => initalState,
},
})
I use name state in component A and then call reset reducer from component B. Even if the state is updated, component A will not re-render. Why is that? I wonder if this initialState function is not updating relevant content.
Do ensure that the
reduceris created the right way. In your given code, the reset reducer has not been created correctly.Here is how you should do it:
Also, do ensure you are dispatching this correctly: