I am trying to access the state.X of my store when Redux-watch store.subscribe() is triggered (by updating state.Y), but I can access just initial state values. I am using Redux-toolkit, React-redux, React-watch
When I call the "print" function after I press the UI button, my state is displayed with desired values, but when I call the "print" function in Redux-watch subscribe() function, the state.X has its initial values. Any idea what can be wrong?
Getting state from the store.
const date = useSelector((state) => state.calendar);
Print function: const print = () =>{console.log(date);}
Redux-watch subscribe function:
let wData = watch(store.getState, 'calendar.monthData');
const unsubscribeWData = store.subscribe(wData((newVal, oldVal, objectPath) =>
{
if(newVal !== oldVal)
{
console.log(newVal); //array with new values
print(); //state has initial values
}
}));
Redux-toolkit Slice:
const calendarSlice = createSlice({
name: "calendar",
initialState: {
date: "", //state.X
monthData: [], //state.Y
},
reducers: {
currentDateAction: (state,action) =>
{
const { newDate} = action.payload;
state.date = newDate;
}
},
extraReducers: builder => {
builder.addCase(fetchCalendarMonthSum.fulfilled, (state, action) => {
state.monthData = action.payload;
})
}
});
export const {currentDateAction} = calendarSlice.actions;
export default calendarSlice.reducer;