gpt4 book ai didi

reactjs - 添加 "Text"作为控制组件后 React Informed eslint 关联控制错误

转载 作者:行者123 更新时间:2023-12-04 17:44:51 24 4
gpt4 key购买 nike

我正在尝试将 informed 包添加到我的项目中,但是当我像这样添加组件时,eslint 出现错误:

    <Form id="intro-form">
<label htmlFor="intro-name">
First name:
<Text field="name" id="intro-name" />
</label>
<button type="submit">Submit</button>
</Form>

我已将 Text 作为 controlComponent 添加到我的 .eslintrc 中,但我仍然收到错误消息:

eslint] 表单标签必须具有以下所有类型的关联控件:嵌套、id (jsx-a11y/label-has-for)

我猜这不是将其添加到我的 .eslintrc 文件的正确方法?

{
"rules": {
"jsx-a11y/label-has-associated-control": [ 2, {
"labelComponents": ["label"],
"labelAttributes": ["htmlFor"],
"controlComponents": ["Text"]
}]
},
"parser": "babel-eslint",
"extends": [
"airbnb"
]
}

当我将 Text 更改为 input 时,错误消失了,所以感觉我误解了它是如何工作的。关于如何允许 Text 作为可接受的 input 有什么建议吗?

最佳答案

label-has-for在 v6.1.0 中被弃用。使用 label-has-associated-control相反。

但是,为了提供答案,components 选项确定应检查哪些 JSX 元素是否具有 htmlFor 属性,在您的情况下,从提供的信息中不清楚。

for some

// .eslintrc
"rules": {
"jsx-a11y/label-has-for": [ 2, {
"components": [ "Label" ],
"required": {
"some": [ "nesting", "id" ]
}
}]
}

// Label component
const Label = ({htmlFor, label}) => <label htmlFor={htmlFor}>{label}</label>

// usage
<Label htmlFor="test" label="label" />
<input id="test"></input>

for every

// .eslintrc
"jsx-a11y/label-has-for": [ 2, {
...
"required": {
"every": [ "nesting", "id" ]
}
}]

// usage
<Label htmlFor="test" label="label">
<input id="test"></input>
</Label>

turn off deprecated rule

// .eslintrc
"rules": {
"jsx-a11y/label-has-for": "off",
"jsx-a11y/label-has-associated-control": [ 2, {
"labelComponents": [ "Label" ],
"labelAttributes": ["label"],
"required": "either"
}]
}

关于reactjs - 添加 "Text"作为控制组件后 React Informed eslint 关联控制错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52595264/

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