react 子组件调用父组件方法

时间:2024-09-20 11:05:44

import React from 'react'
import '../page1/header.css'
import { Table } from 'antd'
import Child from './child'//引入的子组件

export default class Header extends React.Component{
  constructor(){
  super()

  }

}

MakeMoney(){
  alert("我在学习react!");
}

render(){

  return (<div>

    <Child MakeMoney={this.MakeMoney} />

  </div>

}

//子组件

import React from 'react'

export default class Header extends React.Component{
  constructor(props){
  super(props)
 
}
StudyMakeMoney=()=>{ // 调用父组件方法
  this.props.MakeMoney();
}
render(){
console.log(this.props.msg)
return (
  <div className="header">
    <button onClick={this.StudyMakeMoney}>调用父组件中的方法</button>  //点击按钮通过StudyMakeMoney方法中this.props.MakeMoney();
  </div>
)
}
}