子组件调用父组件的方法,从而改变父组件中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> </> }
原文地址:https://www.cnblogs.com/pcxhahaha/p/12359541.html
时间: 2024-11-06 07:17:37