how to resolve this error "ERROR TypeError: Cannot delete property '0' of [object Array]"

5.4k Views Asked by At
on(deleteAnswerAction, (s, { answerId }) => {
    const findAnswerIndex = s.findIndex((item) => item.id === answerId);
    return s.splice(findAnswerIndex, 1);
  }),

this is my reducer on dispatching action deleteAnswer it should delete the answer with respective answerId.

1

There are 1 best solutions below

1
Anton Marinenko On

Because of use state manager the main paradigm here: immutability. You can't mutate your state, you can set a new state only. Splice changes the original data source.

Solution:

...
  const newState = [...s].splice(findAnswerIndex, 1);

  return newState;
}),