父传子-类组件和函数组件

 import React, { Component } from 'react'
 
 class ChildCpn extends Component{
 
   render(){ 
    //  console.log(this.props);  // {name: "Eric", age: "28"}
    let {name,age} = this.props;
      return (
        <h2>子组件展示数据: {name}-{age}</h2> 
      )
   } 
 }
   
 export default class App extends Component {  
   render() { 
     return (
       <div> 
          <ChildCpn name="Eric" age="28"/>
          <ChildCpn name="kebe" age="40"/>
       </div>
     )
   } 
 }
 -----------------------------------
 import React, { Component } from 'react'
 
 function ChildCpn(props){ 
      // console.log(props);  // {name: "Eric", age: "28"} 
    let {name,age} = props;
    return (
      <h2>子组件展示数据: {name}-{age}</h2> 
    ) 
 }
   
 export default class App extends Component {  
   render() { 
     return (
       <div> 
          <ChildCpn name="Eric" age="28"/>
          <ChildCpn name="ker" age="45"/>
       </div>
     )
   } 
 }
 
posted @ 2021-08-10 17:56  13522679763-任国强  阅读(62)  评论(0)    收藏  举报