react子组件调用父组件的方法

class Parent extends Component {
  handleSth () {
    console.log('触发父组件成功')
  }
  render () {
    return (
      <div>
        <Child childHandleSth={this.handleSth}></Child>
      </div>
    )
  }
}

export default Parent
class Child extends Component {
  constructor (props) {
    super(props)
    this.state = {}
  }
  render () {
    return (
      <div onClick={() => this.props.childHandleSth()}>子组件</div>
    )
  }
}
export default Child

 

posted @ 2020-02-23 20:38  panchunxin  阅读(474)  评论(0)    收藏  举报