react typescript 子組件調用父組件


//父組件

import * as React from 'react'
import { Input } from 'antd'
const Search = Input.Search
import "./index.less"
import Child from "./compon/list"
interface IProps {

MakeMoney?: () => void //暴露方法
}
export default class ProjectList extends React.Component<IProps>{
constructor(props: IProps) {
super(props)

}
MakeMoney(){  //子組件調用父組件的方法
alert("我在學習react!");
}
render(){
return (
<div>
<div className="Input_box">
<div style={{ marginBottom: 16 }}>
<Search
placeholder="搜索"
onSearch={value => console.log(value)}
onChange={this.Inpchange}
enterButton
/>
<button>點擊切換</button>
</div>
</div>
<Child MakeMoney={this.MakeMoney}/>//子組件
</div>
)
}
}
//子組件
import * as React from 'react'
import { Row, Col } from 'antd';
import "./list.less"
interface IProps {
msg?: any
MakeMoney?:any  //獲取暴露的方法
}
interface IState {
lg?: any

}
export default class List extends React.Component<IProps, IState> {
constructor(props: IProps) {
super(props)

}
StudyMakeMoney=()=>{ // 調用父組件方法
this.props.MakeMoney();
}
render(){
const { lg } = this.state;
return (
<div>
<button onClick={this.StudyMakeMoney}>子組件</button>
  </div>

}


免責聲明!

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



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