react child component calls the parent component method

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

 

Guess you like

Origin www.cnblogs.com/pcxhahaha/p/12354106.html