reactjs - 反应道具与儿童。什么时候用?
问题描述
这两种方法有什么区别?
将按钮的文本作为道具传递
<TextComponent title="Save"/>
function TextComponent(props){
return <button>{props.title}<button/>
}
对比
小时候传递文本
<TextComponent>Save<TextComponent />
function TextComponent(props){
return <button>{props.children}<button/>
}
解决方案
children
当您提前不知道它们时,您应该使用它们,请参阅: https ://reactjs.org/docs/composition-vs-inheritance.html
在这里,如果您知道将在子组件中使用标题,只需使用命名道具。
如果你问自己这个问题,我会说:“好的,但是那个通用组件会呈现什么?” 是你应该使用的时候children
推荐阅读
- javascript - 绝对值中的 JavaScript 网格
- angular - 禁用(单击)划分内的元素 - Angular/Typescript
- c# - 无法将 excel 列值复制到另一个 excel 文件中的指定范围
- ruby-on-rails - rails 5 中 time_field 中的默认值
- javascript - 为什么当我用 JavaScript 嵌入 PHP 时,我的 PHP 没有激活?
- python - Python 正则表达式:使用 re.sub 在字符串中添加“-”
- python-2.7 - 未收集使用 PyInstaller 打包的 Pytest 测试
- spring-boot - 无法在springboot中使用户具有不同的角色
- php - 我想显示数据库中的所有图像,但我只能显示一个
- wcf - 如何在 UWP 中通过代码隐藏引用 WCF 服务?