简体   繁体   English

如何将子组件转移到组件?

[英]How to transfer children component to component?

I want to render in my Text component another component using: {this.props.child}我想在我的Text组件中渲染另一个组件,使用: {this.props.child}

What should I choose and why?我应该选择什么,为什么?

 <Route path={'/test'} render={() => <Test children={Test1} />} />

or或者

 <Route path={'/test'} render={() => <Test><Test1 /></Test>} />

The children prop should always be passed like: children道具应该总是像这样传递:

<Test><Test1 /></Test>

You may want other JSX passed as a prop sometimes though, like when setting a smaller bit of content somewhere, like a title .不过,有时您可能希望其他 JSX 作为道具传递,例如在某处设置较小的内容时,例如title You can then even use both forms:然后,您甚至可以同时使用 forms:

<Test title={<h1>Big title</h1>}>
  <ChildrenComponent />
</Test>

But, for consistency, the children prop should always be the component's actual children.但是,为了保持一致性, children属性应该始终是组件的实际子项。

You have to use the second way:您必须使用第二种方式:

<Route path={'/test'} render={() => <Test><Test1 /></Test>} />

The render prop provides the ability for inline rendering and passing extra props to the element. render prop 提供了内联渲染和将额外的 props 传递给元素的能力。 This prop expects a function that returns a React element when the current location matches the route's path.这个 prop 需要一个 function 当当前位置与路由的路径匹配时返回一个 React 元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM