Accepted answer

The culprit can be found in your reducer, specifically these lines: = + 1;

The reducer here is both mutating the state, and returning a new state. Mutating the state is bad practice, and can cause weird effects (like the one you're experiencing)

So the '2. has added once' test will increment the id to 0. This will stay in memory during the next test where initialStatus will have an id of 0, not -1. In our reducer, state points to initialStatus. So calling = + 1; increments initialStatus's id field, and same for list. This is why it appears that the ADD action being called three times.

Moving away from mutations in the reducer can help fix this problem.

case "ADD": {
    const newId = + 1;

    return {
      id: newId,
      list: state.list.concat([newId])

Remember, never mutate the state in a reducer, always return the new state instead! (

Related Query

More Query from same tag