From b347e9a3eb935e89ca30797ba253cd299b2a1e57 Mon Sep 17 00:00:00 2001 From: Robert Katzki Date: Fri, 11 Mar 2016 09:15:02 +0100 Subject: [PATCH] fix: remove isMounted antipattern Closes #71 --- src/Geosuggest.jsx | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Geosuggest.jsx b/src/Geosuggest.jsx index fcc73e28..30b898a1 100644 --- a/src/Geosuggest.jsx +++ b/src/Geosuggest.jsx @@ -25,11 +25,11 @@ class Geosuggest extends React.Component { constructor(props) { super(props); this.state = { - isMounted: false, isSuggestsHidden: true, userInput: this.props.initialValue, activeSuggest: null, - suggests: [] + suggests: [], + timer: null }; } @@ -65,15 +65,13 @@ class Geosuggest extends React.Component { this.autocompleteService = new googleMaps.places.AutocompleteService(); this.geocoder = new googleMaps.Geocoder(); - - this.setState({isMounted: true}); } /** * When the component will unmount */ componentWillUnmount() { - this.setState({isMounted: false}); + clearTimeout(this.state.timer); } /** @@ -205,11 +203,11 @@ class Geosuggest extends React.Component { */ hideSuggests() { this.props.onBlur(); - setTimeout(() => { - if (this.state && this.state.isMounted) { - this.setState({isSuggestsHidden: true}); - } + const timer = setTimeout(() => { + this.setState({isSuggestsHidden: true}); }, 100); + + this.setState({timer}); } /**