我已经安装成功了eslint-插件测试库 https://github.com/testing-library/eslint-plugin-testing-library并使用overrides
所以它只警告我测试文件中的代码。
然而,它抱怨Avoid direct Node access. Prefer using the methods from Testing Library.
对于以下代码props.children
:
我希望能够插入子节点或默认节点。
return (
<>
{ // Other elements here. }
{'children' in props ? (
props.children
) : (
<MyComponent {...props} disabled={disabled} />
)}
</>
)
这段代码有什么问题?为什么 props 被认为是节点访问?我应该如何更改它以满足警告?只需添加// eslint-disable-next-line testing-library/no-node-access
?
Edit:
这是在测试文件中。它以与主代码相同的方式创建一个元素。我不明白为什么要引用props.children
会受到警告。我想知道这个警告的理由是什么,以及如何实现预期的结果。
作者eslint-plugin-testing-library
这里!对于所报道的这种特定行为,我深表歉意。的确,props.children
是有效的用法,不应报告。您可以...吗将此报告为新问题 https://github.com/testing-library/eslint-plugin-testing-library/issues/new?我们以后可以考虑改进这个规则。谢谢!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)