标识符 ID 已经在 reducer 中声明

Bom*_*ber 2 reactjs react-redux

我的 . 出现上述错误reducer,如何id在两种开关情况下使用 ?

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;
    });
Run Code Online (Sandbox Code Playgroud)

Piy*_*ush 11

简单.... 只需用大括号 {} 包裹每个 case 的代码

例如

case '1': { your code here }
case '2': { your code here }
Run Code Online (Sandbox Code Playgroud)

let 有块作用域。因为只有一个块以 switch 开头。因此,所有变量都保留在整个 switch 块中。

在每种情况下插入大括号 {} 后,它们将作为变量的范围。作用域结束后,变量不能再使用。