如何使用 testing-library/no-node-access 访问 props.children

How to access props.children with testing-library/no-node-access

我已经成功安装了 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 是一种有效用法,不应报告。你能report this as a new issue吗?我们可以看看将来改进这个规则。谢谢!