I'm setting up a reducer using Redux Toolkit & Immer using the guide here. Using this we can do immutable updates to the state which look like mutations.
However, it's not clear to me if I'm allowed to "mutate" the payload too, as with the example below:
export default interface Song {
name: string;
content: string;
lastPopulated: number;
}
const initialState = {
songs: [] as Song[]
}
const updateSong = createAction<Song>('song/updateSong');
const rootReducer = createReducer(initialState, (builder) => {
builder
.addCase(updateSong, (state, action) => {
const newSong = action.payload;
newSong.lastPopulated = Date.now(); // Is this allowed???????
state.songs = state.songs.map(
song => song.name === newSong.name ? newSong : song
);
})
});
Is that permitted or do I instead need to do something like this?:
const newSong = {
...action.payload
lastPopulated = Date.now();
}