子组件如何改变父组件的state

子组件调用父组件的方法,从而改变父组件中state中的值

//父组件
change (value) {
    console.log(value)
    this.setState({
      red: value.red
    })
    console.log(this.state)
  }
render () {
    return (
      <>
        {this.state.red && this.state.red == 1 ? (
          <div>red == 1</div>
        ) : null}
        <Child childChangeFather={this.change.bind(this)}></Child>
      </>
    )
  }
//子组件
render () {
    <>
      <div onClick={() => {this.props.childChangeFather({red: 1})}}>子组件如何改变父组件的state</div>
    </>
  }

猜你喜欢

转载自www.cnblogs.com/pcxhahaha/p/12359541.html