Last active
November 12, 2018 09:33
-
-
Save chowryan/fea1de1f160947e6a9ff5840fbf7d381 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