reactjs - 为什么不应该将 child 作为 Prop 传递?

标签 reactjs eslint

根据 ESLINT 规则 no-children-prop

When using JSX, the children should be nested between the opening and closing tags. When not using JSX, the children should be passed as additional arguments to React.createElement



这只是一种风格建议,还是会产生真正的损害?

最佳答案

这似乎更像是一种风格建议,也可以防止您射中自己的脚。

如果你有一个组件:

<Component children="foo">bar</Component>

并在组件中做
return (
  <div>
    {this.props.children}
  </div>
)

你最终会得到 bar 作为 this.props.children 而不是 foo 但只要这会破坏任何 react 或造成“损坏”
我没有发现这样的证据,这里有一个冗长的讨论 https://github.com/airbnb/javascript/issues/1089#issuecomment-250640742
说明相同。

关于reactjs - 为什么不应该将 child 作为 Prop 传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42982416/

相关文章:

javascript - 我可以为同一个 es lint 规则执行多个级别吗?例如 : error and warning for different paths

typescript - Eslint 在我的 React 应用程序中不起作用

javascript - 在 react 中从父组件调用子组件的方法是否反模式,为什么?

reactjs - 操作必须是普通对象,使用自定义中间件进行异步操作

node.js - 使用 UTF-8 的 Axios setRequestHeader 方法

javascript - Visual Studio : eslint errors underlined in red

javascript - "ESLint no-loop-func rule"回调中需要循环变量怎么办

reactjs - 无法使用 TypeScript 导入本地字体

javascript - 在子安装上设置父状态是一种反模式吗?

reactjs - 找不到 'bson' 的类型定义文件。该文件在程序中,因为 : Entry point for implicit type library 'bson'