Skip to content

Instantly share code, notes, and snippets.

@whisher
Created September 23, 2018 17:13
Show Gist options
  • Save whisher/d70f38cb7cb326de3ec06f8d64de6747 to your computer and use it in GitHub Desktop.
Save whisher/d70f38cb7cb326de3ec06f8d64de6747 to your computer and use it in GitHub Desktop.
class App extends Component {
_isMounted = false;
constructor(props) {
...
}
...
componentDidMount() {
this._isMounted = true;
const { searchTerm } = this.state;
this.setState({ searchKey: searchTerm });
this.fetchSearchTopStories(searchTerm);
}
componentWillUnmount() {
this._isMounted = false;
}
... }
fetchSearchTopStories(searchTerm, page = 0) {
axios(`${PATH_BASE}${PATH_SEARCH}?${PARAM_SEARCH}${searchTerm}&${PARAM_PAGE}${pa\
ge}&${PARAM_HPP}${DEFAULT_HPP}`)
.then(result => this._isMounted && this.setSearchTopStories(result.data))
.catch(error => this._isMounted && this.setState({ error }));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment