繁体   English   中英

JSX中的ESLint额外括号

[英]ESLint Extra Parentheses In JSX

我注意到在jsx文件中使用逻辑AND(&&)将多余的括号添加到多行条件渲染中。 例如,此代码来自React文档...

{unreadMessages.length > 0 &&
  <h2>
    You have {unreadMessages.length} unread messages.
  </h2>
}

...将进行如下修改:

{unreadMessages.length > 0 && (
  <h2>
    You have {unreadMessages.length} unread messages.
  </h2>
)}

这是我的ESLint配置

"eslintConfig": {
  "root": true,
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "impliedStrict": true
    }
  },
  "env": {
    "browser": true,
    "node": true,
    "es6": true,
    "mocha": true
  },
  "plugins": [
    "react"
  ],
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended"
  ],
  "rules": {
    "computed-property-spacing": [
      "error"
    ],
    "indent": [
      "error",
      2
    ],
    "jsx-quotes": [
      "error"
    ],
    "key-spacing": [
      "error"
    ],
    "no-case-declarations": [
      "off"
    ],
    "no-console": [
      "off"
    ],
    "no-var": [
      "error"
    ],
    "object-curly-spacing": [
      "error",
      "always"
    ],
    "prefer-const": [
      "error"
    ],
    "quotes": [
      "error",
      "single",
      {
        "avoidEscape": true,
        "allowTemplateLiterals": true
      }
    ],
    "react/no-children-prop": "off",
    "react/prop-types": "off",
    "semi": [
      "error",
      "never"
    ]
  }
}

是我无意间造成了这种情况,还是有充分的理由呢? 如果没有,我该如何预防? 禁止不必要的括号似乎过大了。

看起来这是由react / jsx-wrap-multilines引起的 ,可以通过将logical语法类型设置为"ignore"来防止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM