首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用修复由流函数类型引起的警告?

如何使用修复由流函数类型引起的警告?
EN

Stack Overflow用户
提问于 2017-05-01 21:30:32
回答 2查看 1.4K关注 0票数 5

我收到关于我的react组件的下面一行的警告

代码语言:javascript
复制
handleToggle: Function;

我正在使用eslint插件反应,并且收到了一个警告:"handleToggle应该放在构造函数后面“。这与规则反应/排序-comp有关。我在我的.eslintrc.json上尝试了下面的内容

代码语言:javascript
复制
 "react/sort-comp": [1, {
  "order": [
    "static-methods",
    "lifecycle",
    "everything-else",
    "render"
  ],
  "groups": {
    "lifecycle": [
      "displayName",
      "propTypes",
      "contextTypes",
      "childContextTypes",
      "/^.*: Function$/",
      "mixins",
      "statics",
      "defaultProps",
      "state",
      "constructor",
      "getDefaultProps",
      "getInitialState",
      "getChildContext",
      "componentWillMount",
      "componentDidMount",
      "componentWillReceiveProps",
      "shouldComponentUpdate",
      "componentWillUpdate",
      "componentDidUpdate",
      "componentWillUnmount"
    ]
  }
}]

但我无法修正警告。我希望构造函数之前的函数类型与其他类型定义相同。我怎样才能做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-03 17:36:11

问题是eslint-plugin-react没有意识到流,所以没有“类型定义”的组。您可以通过将"everything-else"移动到组件的顶部(在"static-methods"之前,但这也允许您在constructor之前定义任何函数或实例变量),从而使您可以将类型定义放在组件的开头。

ie,将.eslintrc.json更改为:

代码语言:javascript
复制
 "react/sort-comp": [1, {
  "order": [
    "everything-else",
    "static-methods",
    "lifecycle",
    "render"
  ],
  "groups": { /* ... */ }
 }]
票数 0
EN

Stack Overflow用户

发布于 2018-08-22 12:39:19

现在可以向配置中的order部分添加一个“新”项(type-annotations)*:

代码语言:javascript
复制
"react/sort-comp": [
  2,
  {
    "order": [
      "type-annotations",  // <-- this is "new"
      "static-methods",
      "lifecycle",
      "everything-else",
      "render"
    ],
    "groups": {
      "lifecycle": [
        "displayName",
        "propTypes",
        "contextTypes",
        "childContextTypes",
        "mixins",
        "statics",
        "defaultProps",
        "constructor",
        "getDefaultProps",
        "state",
        "getInitialState",
        "getChildContext",
        "getDerivedStateFromProps",
        "componentWillMount",
        "UNSAFE_componentWillMount",
        "componentDidMount",
        "componentWillReceiveProps",
        "UNSAFE_componentWillReceiveProps",
        "shouldComponentUpdate",
        "componentWillUpdate",
        "UNSAFE_componentWillUpdate",
        "getSnapshotBeforeUpdate",
        "componentDidUpdate",
        "componentDidCatch",
        "componentWillUnmount"
      ]
    }
  }
]

在此之后,埃斯林特将停止抱怨。

*在这里发现:https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md#rule-options

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43726814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档