From b16aee75f8f95fa2eba05966c4edef63a5de84bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Damgaard=20M=C3=B8ller?= Date: Fri, 1 Mar 2019 19:35:51 +0100 Subject: [PATCH] State updates may be asynchronous https://reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous --- README.md | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0793532..0df7560 100644 --- a/README.md +++ b/README.md @@ -283,8 +283,21 @@ class Hello extends React.Component { this.state = { currentEnthusiasm: props.enthusiasmLevel || 1 }; } - onIncrement = () => this.updateEnthusiasm(this.state.currentEnthusiasm + 1); - onDecrement = () => this.updateEnthusiasm(this.state.currentEnthusiasm - 1); + onIncrement = () => { + this.setState(nextState => { + return { + currentEnthusiasm: nextState.currentEnthusiasm + 1 + }; + }); + } + + onDecrement = () => { + this.setState(nextState => { + return { + currentEnthusiasm: nextState.currentEnthusiasm - 1 + }; + }); + } render() { const { name } = this.props; @@ -303,10 +316,6 @@ class Hello extends React.Component { ); } - - updateEnthusiasm(currentEnthusiasm: number) { - this.setState({ currentEnthusiasm }); - } } export default Hello;