作为React世界的初学者,我想深入了解当我使用{this.props时发生了什么。Children}和什么情况下使用相同。它在下面的代码片段的相关性是什么?
render() {
if (this.props.appLoaded) {
return (
<div>
<Header
appName={this.props.appName}
currentUser={this.props.currentUser}
/>
{this.props.children}
</div>
);
}
}
在React中,道具。children属性指向传递给React组件的子元素。
例如,在以下代码中:
<MyComponent>
<div>Child 1</div>
<div>Child 2</div>
</MyComponent>
MyComponent组件有两个子元素:一个div元素包含文本“Child 1”,另一个div元素包含文本“Child 2”。MyComponent组件可以使用这些道具访问和呈现这些子组件。孩子们的财产。
例如,MyComponent组件可以像这样呈现它的子组件:
class MyComponent extends React.Component {
render() {
return (
<div>
{this.props.children}
</div>
);
}
}
这将把两个div元素呈现为MyComponent组件的子元素。