Skip to content

Instantly share code, notes, and snippets.

@chowryan
Last active November 12, 2018 09:33
Show Gist options
  • Save chowryan/fea1de1f160947e6a9ff5840fbf7d381 to your computer and use it in GitHub Desktop.
Save chowryan/fea1de1f160947e6a9ff5840fbf7d381 to your computer and use it in GitHub Desktop.
react setstate?
// 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