首页 > 解决方案 > 反应道具与儿童。什么时候用?

问题描述

这两种方法有什么区别?

将按钮的文本作为道具传递

<TextComponent title="Save"/>

function TextComponent(props){
  return <button>{props.title}<button/>
}

对比

小时候传递文本

<TextComponent>Save<TextComponent />

function TextComponent(props){
  return <button>{props.children}<button/>      
}

标签: reactjs

解决方案


children当您提前不知道它们时,您应该使用它们,请参阅: https ://reactjs.org/docs/composition-vs-inheritance.html

在这里,如果您知道将在子组件中使用标题,只需使用命名道具。

如果你问自己这个问题,我会说:“好的,但是那个通用组件会呈现什么?” 是你应该使用的时候children


推荐阅读