加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 如何使用eslint-plugin-react来修复由Flow Function

发布时间:2020-12-15 20:30:19 所属栏目:百科 来源:网络整理
导读:我在反应组件的下一行收到警告 handleToggle: Function; 我正在使用eslint-plugin-react和Flow,我收到警告“handleToggle应该放在构造函数之后”.这与规则react / sort-comp有关.我在.eslintrc.json上尝试了以下内容 "react/sort-comp": [1,{ "order": [ "st
我在反应组件的下一行收到警告

handleToggle: Function;

我正在使用eslint-plugin-react和Flow,我收到警告“handleToggle应该放在构造函数之后”.这与规则react / sort-comp有关.我在.eslintrc.json上尝试了以下内容

"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"
    ]
  }
}]

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

解决方法

问题是eslint-plugin-react不知道Flow,因此没有“类型定义”组.您可以使用eslint将类型定义放在组件的开头,方法是将“everything-else”移动到组件的顶部(在“static-methods”之前),但这也允许您定义任何函数或实例变量(如果你正在使用它们)在构造函数之前.

即,将.eslintrc.json更改为:

"react/sort-comp": [1,{
  "order": [
    "everything-else","static-methods","groups": { /* ... */ }
 }]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读