guys how to append an object to an empty array in redux toolkit? I have set a
currentTimeOfServices:[]
in my slice then I dispatch an action with a payload
dispatch(userActions.getCurrentTime({ startTime: startTime, name: item }));
In my getCurrenTtime reducer I don't understand how to append this item.
getCurrentTime: (state, action) => {
state.currentTimeOfServices = [
...state.currentTimeOfServices,
{ startTime: action.payload.startTime, name: action.payload.name },
];
}
This is wrong and I know that but I want to know how to add/append an object to this currentTimeOfServices empty array?
CodePudding user response:
Redux Toolkit uses Immer inside, which lets you "mutate" the existing state with normal JS syntax. So, all you need is:
state.currentTimeOfServices.push(newItem)
What you've got there is valid for a hand-written immutable update, but it's not necessary here.
See the docs for more details: