父:
import React from "react";
import Son from './son';
class Father extends React.Component {
state = {
content: true
};
makeMoney = (val:boolean) => { // 引入typeScript 所以参数带类型
console.log(val)
this.setState({
cameraVisible: val,
});
};
render() {
return (
<>
<Son content={this.state.content} MakeMoney={this.makeMoney.bind(this)}></Son>
</>
);
}
}
export default Father;
-----------------------------------------------------------------------------------------
子:
import React, { Component } from "react";
class Son extends Component<any> {
constructor(props: any) {
super(props);
this.state = {
content: this.props.content,
};
}
StudyMakeMoney = () => {
this.props.MakeMoney(true); // 父方法
};
render() {
return (
<div>
<div>{this.props.content}</div>
<div onClick={this.StudyMakeMoney}>bb</div>
</div>
);
}
}
export default Son;