Created
April 17, 2017 08:06
-
-
Save BTMPL/81cae986bd93b8c55255e3d110e7efb5 to your computer and use it in GitHub Desktop.
Understanding 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
// assuming this.state = { value: 0 } | |
this.setState({ | |
value: 1 | |
}); | |
console.log(this.state.value); // 0 |
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
// assuming this.state = { value: 0 }; | |
this.setState({ value: this.state.value + 1}); | |
this.setState({ value: this.state.value + 1}); | |
this.setState({ value: this.state.value + 1}); |
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
// assuming this.state = { value: 0 }; | |
this.setState((state) => ({ value: state.value + 1})); | |
this.setState((state) => ({ value: state.value + 1})); | |
this.setState((state) => ({ value: state.value + 1})); |
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
render() { | |
return <button onClick={this.inc}>Click to update</button> | |
} | |
inc() { | |
console.log('before: ' + this.state.test); | |
this.setState({ | |
test: this.state.test+1 | |
}); | |
console.log('after: ' + this.state.test); | |
} |
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
// click! | |
before: 1 | |
after: 1 | |
// click! | |
before: 2 | |
after: 2 |
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
class Component extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { value: this.props.value }; | |
} | |
render() { | |
return <div>The value is: {this.state.value}</div> | |
} | |
} |
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
class Component extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { value: this.props.value }; | |
} | |
componentWillReceiveProps(nextProps) { | |
if(nextProps.value !== this.props.value) { | |
this.setState({value: nextProps.value}); | |
} | |
} | |
render() { | |
return <div>The value is: {this.state.value}</div> | |
} | |
} |
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
this.setState((state) => { | |
if(checkSomeConditions()) return undefined; | |
else return { value: 42} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment