加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react typescript 子组件调用父组件

发布时间:2020-12-15 20:14:38 所属栏目:百科 来源:网络整理
导读://父组件 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 Project

//父组件

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>
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读