React子组件怎么改变父组件的state
React子组件怎么改变父组件的state
1.父组件
class Father extends React.Component { construtor(props){ super(props); this.state={ isRed: 0 } } onChangeState(isTrue){ this.setState(isTrue) } render(){ <p>颜色:{this.state.isRed}</p> <Child onClicked={this.onChangeState.bind(this)}/> } }
2.子组件
class Child extends React.Component { render(){ <Button onClicked={()=>this.props.onClicked({isRed: 1})}/> } }