react 子組件調用父組件方法


react 子組件調用父組件中的方法

 

父組件

import React,{Component} from 'react';
import SonClick from './SonCllick/SonClick';
class ParentClick extends Component{
MakeMoney(){
alert("我在掙錢!");
}
render(){
return(
<div>

<div>
<SonClick ref="child" onRef={this.onRef} MakeMoney={this.MakeMoney}></SonClick>
</div>
</div>
)
}
}

export default ParentClick;


子組件

import React,{Component} from 'react';

class SonClick extends Component{
StudyMakeMoney=()=>{ // 學習掙錢,調用父組件方法
this.props.MakeMoney();
}
render(){
return(
<div>
我是子組件

<div>
<button onClick={this.StudyMakeMoney}>調用父組件中的方法</button>
</div>
</div>
)
}
}

export default SonClick;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM