简体   繁体   English

将Jsx组件作为props传递给另一个jsx组件

[英]Passing A Jsx component to another jsx component as props

If I'm passing a jsx component(A) to another component(B) as a prop, like this: 如果我将jsx组件(A)作为prop传递给另一个组件(B),如下所示:

import A from "A.jsx";
import B from "B.jsx";
...
class MyClass extends React{
...
  render(){
    return <B customContent={A} />
  };
}

when component B gets updated in React life-cycle, will A gets updated too? 当组件B在React生命周期中更新时,A也会更新吗?

In React when a component is rendered all its child components will be rendered too . 在React中渲染组件时,它的所有子组件也将被渲染。

In your case when you pass component A to B as an attribute component A will be passed to Component B as a regular object and will not be rendered . 在您将组件A作为属性组件传递给B的情况下, A将作为常规对象传递给组件B ,并且不会呈现。

for ex: if you passed function instead of component A it will not be executed untill you call it from the component B. 例如:如果你传递函数而不是组件A,它将不会被执行,直到你从组件B调用它。

if you need to render a component inside another dynamically you should use children property on the props object let me show you : 如果你需要动态地在另一个内部渲染一个组件,你应该在props对象上使用children属性让我告诉你:

At My Class 在我的班级

  class MyClass extends React{
    ...
      render(){
        return <B > 

                 <A />

               </B>
      };
    }

At Component B 在组件B

You can do the following : 您可以执行以下操作:

 class B extends Component{
    ...
      render(){
        return
                <React.Fragment> 

                 {this.props.children}

               </React.Fragment>
      };
    }

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

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