Created
October 3, 2017 09:39
-
-
Save kennethlynne/e14430e289c5c6c20c87d6f014ab7cdd to your computer and use it in GitHub Desktop.
Component to delay rendering
This file contains hidden or 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
| import * as React from 'react'; | |
| interface IProps { | |
| children: JSX.Element; | |
| delay?: number; | |
| } | |
| interface IState { | |
| wait: boolean; | |
| } | |
| export default class Delay extends React.Component<IProps, IState> { | |
| timer: number; | |
| constructor(props: IProps) { | |
| super(props); | |
| this.state = { | |
| wait: true, | |
| }; | |
| } | |
| componentDidMount() { | |
| this.timer = setTimeout( | |
| () => { | |
| this.setState({ | |
| wait: false, | |
| }); | |
| }, | |
| this.props.delay); | |
| } | |
| componentWillUnmount() { | |
| clearTimeout(this.timer); | |
| } | |
| render(): JSX.Element { | |
| if (!this.state.wait) { | |
| return this.props.children; | |
| } | |
| return null; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment