I'm using React 18 with Vite, Redux Toolkit 2.1.0.
What I'm trying to achieve is to add all events fetched from a MongoDB database to the state of my reducer.
This is part of my calendar segment.
export const calendarSlice = createSlice({
name: 'calendar',
initialState: {
loadingEvents: false,
events: [],
activeEvent: null
},
reducers: {
onLoadEvents: (state, { payload = [] }) => {
state.loadingEvents = true;
payload.forEach(event => {
const exists = state.events.some(dbEvent => dbEvent.id === event.id);
if (!exists) {
state.events.push(event);
}
});
}
}
});
If I do a console.log with the payload I can see all the events from the database, however the push command does not add the events to state.events .
I did a search on previous posts here on the website but still couldn't find a solution to my problem.