//父组件
import Child from './index’
export default class Parent extends Component {
constructor(props) {
super(props);
this.state = {
data:'我是父组件'
}
}
getState = ()=>{
console.log(this.state.data);//此时this.指向的是子组件,输出的是 我是子组件
}
render(){
const _this = this;
return(
<div>
<Child getState ={_this.getState } ></Child>
</div>
)
}
}
//子组件
export default class Child extends Component {
constructor(props) {
super(props);
this.state = {
data:'我是子组件'
}
}
getState = ()=>{
this.props.getState&& this.props.get();
}
render(){
const _this = this;
return(
<div>
<Button onClick={_this.getState } >点我</Button>
</div>
记录:react 子组件调用父组件的方法时,this指向子组件的问题
最新推荐文章于 2023-04-20 17:10:20 发布