【问题标题】:eslint complaining about spread operator in simple React reducer [duplicate]eslint 在简单的 React reducer 中抱怨传播运算符 [重复]
【发布时间】:2020-11-12 09:01:27
【问题描述】:

我有一个非常简单的 React reducer,而 eslint 抱怨我如何传播状态。下面是代码。我预计会没事的。我也在下面粘贴我的 .eslintrc。

function quizReducer(state, action) {
  switch (action.type) {
    case RESET_QUIZ:
      return {
        ...state,
        currentQuestion: 0,
        currentAnswer: '',
      }
    default:
      return state
  }
}

eslintrc

{
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "destructuring": true,
      "spread": true,
      "experimentalObjectRestSpread": true
    }
  },
  "plugins": ["react-hooks"],
  "rules": {
    //additional rules here
    "react-hooks/rules-of-hooks": "error"
  }
}

错误在...状态行并且是

ESLint: Parsing error: Unexpected token ...

【问题讨论】:

  • 抱怨”——你能说得具体一点吗?
  • 抱歉,忘记添加了。我会更新问题(正忙于格式化)

标签: javascript reactjs ecmascript-6 eslint


【解决方案1】:

尝试从 eslintrc 中删除行,在 "jsx": true 之后,你应该得到以下信息:

"parserOptions": {
  "ecmaVersion": 2020,
  "ecmaFeatures": {
    "jsx": true
  }
}

【讨论】:

  • 这不起作用,但很好的提示。我将 jsx 中的所有内容都删除为 true 并更改为“ecmaVersion”:2020,这很有效。请更新您的答案,我会将其标记为正确。
  • 很高兴该提示有效,在我的情况下它甚至可以使用“ecmaVersion”:6
【解决方案2】:

在 ESLint 5.0.0 版本中,the experimentalObjectRestSpread option was deprecated

尝试从您的ecmaFeatures 中删除除jsx 之外的所有选项。还将您的 ecmaVersion 更改为至少年份 2018 或版本 9(或更高版本),如同一链接中所述。

    "ecmaVersion": 2018, // or 9
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
    }

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 2023-04-09
    • 2021-01-16
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    相关资源
    最近更新 更多