-
-
Save AyoAlfonso/8697347c8e914fda23d6cd4c44ad39c0 to your computer and use it in GitHub Desktop.
react setstate?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Method 1: | |
class Parent extends Component { | |
state = { foo: '', bar: '', buzz: '' } | |
render() { | |
return ( | |
<Child | |
setParentState={this.setState} | |
> | |
) | |
} | |
} | |
class Child extends Compoent { | |
render() { | |
return ( | |
<div> | |
<Button onClick={() => setParentState({ foo: 1, bar: 2})}> | |
<Button onClick={() => setParentState({ bar: 3, buzz: 4})}> | |
<div> | |
} | |
} | |
// Method 2: | |
class Parent extends Component { | |
state = { foo: '', bar: '', buzz: '' } | |
render() { | |
return ( | |
<Child | |
setFoo={(foo) => this.setState({ foo })} | |
setBar={(bar) => this.setState({ bar })} | |
setBuzz={(buzz) => this.setState({ buzz })} | |
> | |
) | |
} | |
} | |
class Child extends Compoent { | |
onClickButton = () => { | |
this.props.setFoo(1) | |
this.props.setBar(2) | |
} | |
onCLickButton2 = () => { | |
this.props.setBar(3) | |
this.props.setBuzz(4) | |
} | |
render() { | |
return ( | |
<div> | |
<Button onClick={this.onClickButton}> | |
<Button onClick={this.onClickButton2}> | |
</div> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment