How to replace an array in state in redux?

I was following this Redux tutorial

I wanted to replace the array of objects in the state with a new array of objects in the extra reducer.

Here is my code

	[deleteTodosAsync.fulfilled]: (state, action) => {
			state.todos = action.payload.todo;
		
	}

I am getting error that Immer only supports setting array indices and the ‘length’ property

My guess would be that action.payload.todo is not an array (and if it is, you should think about naming). If I’m correct, then you’re trying to replace todos array with a single todo which is causing the error.

I was able to solve the issue by simply returning the new array and it updated the state

[deleteTodosAsync.fulfilled]: (state, action) => {
		return action.payload.todo;
	
}
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.