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)

}
  
child: any = {}  //主要加这个
onRef = (ref) => {
this.child = ref
}
// 调用组件进行通信
getDS = () => {
this.child.toggle()

}
render(){
return (
  <div>
    <button onClick={this.getDS}>父组件点击切换</button>
    <Child ref={this.onRef} />
  </div>

)
}
}

//子组件
import * as React from 'react'
import { Row, Col } from 'antd';
import "./list.less"
interface IProps {
msg?: any
MakeMoney?:any
//ref?:any

}
interface IState {
lg?: any

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

}
state = {
lg: 6
}
toggle = () => {//父组件要调用的方法
console.log('f')
this.setState({
lg: 12
})
}

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