Created
February 7, 2019 06:23
-
-
Save vldvel/0ca9ea49029b36ea310724eaa06d37d4 to your computer and use it in GitHub Desktop.
setTimeout in componentDidMount
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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