Skip to content

Instantly share code, notes, and snippets.

@vldvel
Created February 7, 2019 06:23
Show Gist options
  • Save vldvel/0ca9ea49029b36ea310724eaa06d37d4 to your computer and use it in GitHub Desktop.
Save vldvel/0ca9ea49029b36ea310724eaa06d37d4 to your computer and use it in GitHub Desktop.
setTimeout in componentDidMount
class NewMessages extends React.PureComponent {
updateTimeout;
componentDidMount() {
this.updateMessages();
}
updateMessages = async () => {
// thunk
await this.props.getNewMessages(); // waiting for request
this.updateTimeout = setTimeout(this.updateMessages, 500);
}
componentWillUnmount() {
clearTimeout(updateTimeout);
}
render() {
return (
<div>
{this.props.newMessages}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment