gpt4 book ai didi

reactjs - 如何使用 eslint-plugin-react 修复由流函数类型引起的警告?

转载 作者:行者123 更新时间:2023-12-03 13:38:24 24 4
gpt4 key购买 nike

我的 react 组件上的以下行收到警告

handleToggle: Function;

我正在使用eslint-plugin-reactFlow我收到一条警告“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"
]
}
}]

但我无法修复该警告。我希望构造函数之前的函数类型与其他类型定义相同。我怎样才能实现这个目标?

最佳答案

您现在可以将"new"项目(类型注释)*添加到配置中的订单部分:

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

此后,eslint 将停止提示。

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

关于reactjs - 如何使用 eslint-plugin-react 修复由流函数类型引起的警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43726814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com