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