eslint - 如何使用 testing-library/no-node-access 访问 props.children
问题描述
我已经成功安装了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
这是一种有效的用法,不应报告。您能将此作为新问题报告吗?我们可以看看将来改进这个规则。谢谢!
推荐阅读
- python - python flask request.args.get 不返回所有
- javascript - 在 Vue.js 中测试路由参数
- python - 如何将具有不同长度的字符串转换为日期时间
- c++ - 将 std::sort 应用于整数向量向量时出现分段错误
- laravel - 在索引视图刀片中隐藏克隆帖子
- python - 如何为需要环境变量的 shell 实用程序创建 brew 自定义水龙头
- view - SAPUI5 属性 mProperties 是 sap.ui.base.ManagedObject 的私有成员
- reactjs - Jest React:如何模拟在 componentDidMount 中设置的窗口 eventListener
- laravel - Laravel 8 包(角色和权限 jeremykenedy/laravel-roles)与 MongoDB UserTableSeeder 不工作
- node.js - 如果代码在子文件夹中,如何将节点应用程序部署到 Heroku?