什么是{this.props.children},何时应该使用它?

237

作为React世界的初学者,我希望深入了解在使用{this.props.children}时会发生什么以及何时使用相同的情况。在下面的代码片段中它的相关性是什么?

render() {
  if (this.props.appLoaded) {
    return (
      <div>
        <Header
          appName={this.props.appName}
          currentUser={this.props.currentUser}
        />
        {this.props.children}
      </div>
    );
  }
}

3
可能是ReactJS:为什么要使用this.props.children?的重复问题。 - maazadeeb
3
在所提供的链接中,使用 {this.props.children} 在组件内部会发生什么并不清楚。 - nim007
最佳资源 https://mxstbr.blog/2017/02/react-children-deepdive/ - Akshay Vijay Jain
8个回答

342

‘children’是什么意思?

React文档说,你可以在代表“通用框”且不知道其子元素的组件上使用props.children。对我来说,这并没有真正澄清问题。我相信对于某些人来说,这个定义很清楚,但对我来说却不是。

我对this.props.children的简单解释是:它用于显示在调用组件时包含在开放和关闭标签之间的任何内容。

一个简单的例子:

这是一个用于创建组件的无状态函数的示例。同样,由于这是一个函数,因此没有this关键字,所以只需使用props.children即可。

const Picture = (props) => {
  return (
    <div>
      <img src={props.src}/>
      {props.children}
    </div>
  )
}

这个组件包含一个 <img>,它接收一些 props,然后显示 {props.children}
每当调用此组件时,{props.children} 也会被显示出来,这只是对组件开头和结尾标签之间内容的引用。
//App.js
render () {
  return (
    <div className='container'>
      <Picture key={picture.id} src={picture.src}>
          //what is placed here is passed as props.children  
      </Picture>
    </div>
  )
}

如果您使用完整的开放和关闭标签<Picture> </Picture>来调用组件,而不是使用自闭合标签<Picture />,则可以在其之间放置更多代码。

这将使<Picture>组件与其内容解耦,并使其更具可重用性。

参考:React的props.children快速介绍


1
@Nimmy 当使用 {props.children} 时,我们不会有性能问题,只需要将组件作为 props 传递。当我们可以在子组件内部使用 children 时,就不需要使用 {props.children}。 - Soroush Chehresa
2
示例来源于 https://codeburst.io/a-quick-intro-to-reacts-props-children-cb3d2fce4891 - Alpit Anand
2
@AlpitAnand 在答案末尾可以看到参考资料 :| - Soroush Chehresa
1
我喜欢你在文本中使用引号的方式。 - dawn
1
这个解释很清晰,谢谢@SoroushChehresa。文档应该更经常使用“简单的解释”... - Corné
显示剩余6条评论

48

props.children 表示在调用/渲染组件时在开放和关闭标签之间的内容:

const Foo = props => (
  <div>
    <p>I'm {Foo.name}</p>
    <p>abc is: {props.abc}</p>

    <p>I have {props.children.length} children.</p>
    <p>They are: {props.children}.</p>
    <p>{Array.isArray(props.children) ? 'My kids are an array.' : ''}</p>
  </div>
);

const Baz = () => <span>{Baz.name} and</span>;
const Bar = () => <span> {Bar.name}</span>;

调用/执行/渲染 Foo:

<Foo abc={123}>
  <Baz />
  <Bar />
</Foo>

props和props.children


7
谢谢。那个展示更简单、更简洁,对我很有帮助。 - Ken Ingram

41

我假设你是在一个 React 组件的 render 方法中看到这个信息,就像这样 (编辑:你修改后的问题确实显示了这一点)

class Example extends React.Component {
  render() {
    return <div>
      <div>Children ({this.props.children.length}):</div>
      {this.props.children}
    </div>;
  }
}

class Widget extends React.Component {
  render() {
    return <div>
      <div>First <code>Example</code>:</div>
      <Example>
        <div>1</div>
        <div>2</div>
        <div>3</div>
      </Example>
      <div>Second <code>Example</code> with different children:</div>
      <Example>
        <div>A</div>
        <div>B</div>
      </Example>
    </div>;
  }
}

ReactDOM.render(
  <Widget/>,
  document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

children 是 React 组件的特殊属性,包含组件内定义的任何子元素,例如上面 Example 中的 divs{this.props.children} 将这些子元素包含在渲染结果中。

...什么情况下使用它?

当您想将子元素直接包含在呈现的输出中且保持不变时使用;反之则不使用。


@Nimmy:抱歉,我不明白你所说的“...比在子组件中使用它更好”的意思。 - T.J. Crowder
1
我的意思是在子组件中调用 {this.props.children} 而不是直接在相应的子组件中编写节点。 - nim007
@Nimmy:对不起,我还是没明白。你能尝试用另一种方式表达上面的代码吗? - T.J. Crowder
3
@Nimmy 我假设你在想为什么要使用{this.props.children},而不是直接把它写下来,我知道这个。如果是这样,请这样做并获得一点性能优势。但是你的组件将是静态的,无法在代码库中的不同位置与不同子项集合一起重用。 - Subin Sebastian
3
@ssk: 哦!不错。尼米 - 是的,你可以直接在render中编写子元素,但这将导致每个实例都是相同的子元素。通过接受子元素(如果适用),您的组件的每个实例都可以具有不同的内容。我已经更新了答案中的示例。 - T.J. Crowder
显示剩余2条评论

11

这个问题已经被回答了,但是我想举一个优化的例子。你可以将内联内容分解,只获取特定的属性,而不是整个props对象(这意味着你不必一遍又一遍地写props)。

const MyView = ({title, children}) => {
  return (
    <>
      <h1>{title}</h1>
      {children}
    </>
  );
}

然后您可以这样使用它:

import { MyView } from './MyView';

const MyParentView = () => {
  return (
    <MyView title="Hello">
      <h2>World</h2>
    </MyView>
  );
}

最终的结果将是 HTML,呈现出类似于以下内容的效果:
<div>
  <h1>Hello</h1>
  <h2>World</h2>
</div>

1
非常感谢。回答非常好。对于初学者来说,简短、简单、易于理解... - TomDom

3

“Children”是一个保留名称,“children prop”指的是自定义组件开放和关闭标签之间的内容。在这种情况下,开放和关闭标签之间的内容将会被作为“children prop”的值,在该组件内部可以使用。


2
对于一个孩子来说,有一个父母,就像在React官方示例中所看到的那样,这个父母是FancyBorderh1pFancyBorder的孩子。您可以在CodePen链接上实践检查它。

enter image description here


0
在React中,props.children属性指的是传递给React组件的子元素。
例如,在以下代码中:
<MyComponent>
  <div>Child 1</div>
  <div>Child 2</div>
</MyComponent>

MyComponent组件有两个子元素:一个包含文本“Child 1”的div元素和一个包含文本“Child 2”的div元素。这些子元素可以通过props.children属性访问和渲染MyComponent组件。

例如,MyComponent组件可以像这样渲染其子元素:

class MyComponent extends React.Component {
  render() {
    return (
      <div>
        {this.props.children}
      </div>
    );
  }
}

这将把两个 div 元素渲染为 MyComponent 组件的子元素。


0
这里需要注意的重要事情是,children是一种特殊的prop,用于将数据从父组件传递到子组件,但这些数据必须包含在父组件的开放和关闭标签内。这主要用于某些包装器组件中,我们必须将数据传递到下一个组件,并且我们传递的数据是静态数据(在大多数情况下),因为对于动态数据,有另一种将props传递给组件的方法。

这里是示例


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接