[英]How to fix warning caused by Flow Function Types using eslint-plugin-react?
I'm getting a warning on the following line on my react component 我在反应组件的下一行收到警告
handleToggle: Function;
I'm using eslint-plugin-react and Flow and I'm getting a warning "handleToggle should be placed after constructor". 我正在使用eslint-plugin-react和Flow ,我收到警告“handleToggle应该放在构造函数之后”。 This is related to rule react/sort-comp.
这与规则react / sort-comp有关。 I tried with the following on my .eslintrc.json
我在.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"
]
}
}]
But I'm unable to fix the warning. 但是我无法修复警告。 I want the Function Types before constructor the same as the other Type Definition.
我希望构造函数之前的函数类型与其他类型定义相同。 How can I achieve this?
我怎样才能做到这一点?
you can now add a "new" item ( type-annotations
)* to the order section in the config: 您现在可以在配置中的订单部分添加“新”项(
type-annotations
)*:
"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"
]
}
}
]
after this, eslint will stop complaining. 在此之后,eslint将停止抱怨。
*
found here: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md#rule-options *
在这里找到: https : //github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md#rule-options
The problem is that eslint-plugin-react
is not aware of Flow, so there is no group for "type definitions". 问题是
eslint-plugin-react
不知道Flow,因此没有“类型定义”组。 You can make eslint let you place your type definitions at the beginning of your component by moving "everything-else"
to the top of your component (before "static-methods"
, but that will also allow you to define any functions or instance variables (in case you're using them) before the constructor
. 您可以使用eslint将类型定义放在组件的开头,方法是将
"everything-else"
移动到组件的顶部(在"static-methods"
,但这也允许您定义任何函数或实例变量(如果你正在使用它们)在constructor
之前。
ie, change your .eslintrc.json
to: 即,将
.eslintrc.json
更改为:
"react/sort-comp": [1, {
"order": [
"everything-else",
"static-methods",
"lifecycle",
"render"
],
"groups": { /* ... */ }
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.