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

猜你喜欢

转载自www.cnblogs.com/pcxhahaha/p/12354106.html
今日推荐