From 9baa394b0fa6e89ba339d827d30df4b41f139a9b Mon Sep 17 00:00:00 2001 From: Robin Berjon Date: Thu, 15 Sep 2016 18:20:54 -0400 Subject: [PATCH] clear timeout before changing state to avoid race condition --- src/components/TypeWriter.jsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/TypeWriter.jsx b/src/components/TypeWriter.jsx index aeadec8..537da97 100644 --- a/src/components/TypeWriter.jsx +++ b/src/components/TypeWriter.jsx @@ -20,7 +20,7 @@ class TypeWriter extends React.Component { } componentWillUnmount() { - clearInterval(this._timeoutId); + clearTimeout(this._timeoutId); } componentWillReceiveProps(nextProps) { @@ -28,10 +28,12 @@ class TypeWriter extends React.Component { const active = this.props.typing; if (active > 0 && next < 0) { + clearTimeout(this._timeoutId); this.setState({ visibleChars: this.state.visibleChars - 1 }); } else if (active <= 0 && next > 0) { + clearTimeout(this._timeoutId); this.setState({ visibleChars: this.state.visibleChars + 1 });