0

程序:-

import React from 'react'; 
import ReactDOM from 'react-dom'; 

// Parent Component 
class Parent extends React.Component{ 
    render(){ 
        return( 
                <div> 
                    <h2>You are inside Parent Component</h2> 
                    <Child name="User" userId = "5555"/> 
                </div> 
            ); 
    } 
} 

// Child Component 
class Child extends React.Component{ 
    render(){ 
        console.log(this.props); 
        return( 
                <div> 
                    <h2>Hello, {this.props.name}</h2> 
                    <h3>You are inside Child Component</h3> 
                    <h3>Your user id is: {this.props.userId}</h3> 
                </div> 
            ); 
    } 
} 

ReactDOM.render( 
    // passing props 
    <Parent />, 
    document.getElementById("root") 
); 

我可以看到使用渲染方法调用了父类 dint 知道如何调用类组件子组件。

4

1 回答 1

0

这是来自 React 文档的解释:

组件可以在其输出中引用其他组件。这让我们可以对任何细节级别使用相同的组件抽象。一个按钮、一个表单、一个对话框、一个屏幕:在 React 应用程序中,所有这些通常都表示为组件。

你的Parent组件指的是Child组件;当Parent被渲染时,Child也是,带有道具nameuserId.

组成组件

于 2020-05-20T20:23:22.793 回答