标识符ID已在reducer中声明

时间:2019-07-04 14:10:14

标签: 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;
    });

1 个答案:

答案 0 :(得分:1)

简单...。只需将每种情况的代码都用花括号{}包裹

例如

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

let具有块范围。由于只有一个以开关开头的块。因此,所有变量都保留在整个开关块中。

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