For example I have following RTX setup:
export interface UserState {
name: string
}
const initialState: UserState = {
name: '',
}
const slice = createSlice({
name: 'user',
initialState,
reducers: {
setUserName: (state, action: { payload: string}) => {
state.name = action.payload
}
},
})
export const { setUserName } = slice.actions
export default slice.reducer
export const store = configureStore({
reducer: {
user: userReducer,
}
})
I know that I can use
store.subscribe(() => {
const currentState = store.getState()
})
But this is called for all reducers in the store. I want to just listen to user reducer’s name state. Is it possible?
2
Answers
useage useSelector
You will be able to get userName
Yes, you can manually extract the state like the example below.