为什么不应该把孩子当作道具? [英] Why shouldn't children be passed as a prop?

查看:59
本文介绍了为什么不应该把孩子当作道具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据ESLINT规则 no-children-prop

According to ESLINT rule no-children-prop

使用JSX时,子项之间应嵌套在开口之间和结束标签.不使用JSX时,子级应作为React.createElement的其他参数

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

这仅是一种风格建议,还是会造成真正的损害?

Is this just a style suggestion, or can real damage arise?

推荐答案

这似乎是一种风格上的建议,并且还会阻止您用脚射击.

It seems to be more of a stylistic suggestion and also prevents you from shooting yourself in the foot.

如果您有组件:

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

和组件中的

return (
  <div>
    {this.props.children}
  </div>
)

您最终将获得 bar this.props.children 而不是 foo ,但就此而言,这破坏了任何反应或导致损害"我没有发现这样的证据,这里有很长的讨论 https://github.com/airbnb/javascript/issues/1089#issuecomment-250640742 说明相同.

you would end up getting bar as this.props.children instead of foo but as far as this breaking anything in react or causing "damage" I have found no evidence of such and there is a lengthy discussions here https://github.com/airbnb/javascript/issues/1089#issuecomment-250640742 stating the same.

这篇关于为什么不应该把孩子当作道具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆