Bomber
Bomber

Reputation: 10967

Identifier ID already been declared in reducer

I am getting the above error with my reducer, how can I use the id in both switch cases?

case "SUBMIT_ANSWER":
    const { current, results, completed, id } = action.data;

    return state.map(video =>
        video.id === id
            ? { ...video, current, results, completed }
            : video
    );
case "RESET_QUIZ":
    const { id } = action;
    console.log("action", action);
    console.log("state", state);
    return state.map(video => {
        video.id == id
            ? {
                  ...video,
                  completed: false,
                  current: 0,
                  results: {
                      correctAnswers: 0,
                      score: 0
                  },
                  totalScore: 0
              }
            : video;
    });

Upvotes: 2

Views: 690

Answers (1)

Piyush
Piyush

Reputation: 3285

Simple.... just wrap the code of each case in curly braces {}

For e.g.

case '1': { your code here }
case '2': { your code here }

let has block scope. Since there is only one block which starts with switch. Thus all the variables persist in the whole switch block.

After inserting braces {} in each case, they will work as a scope of your variables. After scope is over the variable cannot be used after that.

Upvotes: 11

Related Questions