首页 > 其他 > 详细

react 父组件调用子组件中的事件

时间:2018-09-06 15:40:41      阅读:238      评论:0      收藏:0      [点我收藏+]

import React, {Component} from ‘react‘;

export default class Parent extends Component {

  render() {

    return(

        <div> <Child onRef={this.onRef} />

        <button onClick={this.click} >click</button>

        </div>

    )

  }

  onRef = (ref) => {

    this.child = ref

  }

  click = (e) => {

    this.child.myName()

  }

}

 

class Child extends Component {

constructor(props){
super(props);
this.state = {value: ‘‘};
}

  componentDidMount(){

    this.props.onRef(this)

  }

  myName = () => {

    this.setState({value:‘boonook‘});

  }

render(){
return(
<div>
我是子组件
<input value={this.state.value} type="text"/>
</div>
)
}

}

react 父组件调用子组件中的事件

原文:https://www.cnblogs.com/boonook/p/9598470.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!