Skip to content

Instantly share code, notes, and snippets.

@kennethlynne
Created October 3, 2017 09:39
Show Gist options
  • Select an option

  • Save kennethlynne/e14430e289c5c6c20c87d6f014ab7cdd to your computer and use it in GitHub Desktop.

Select an option

Save kennethlynne/e14430e289c5c6c20c87d6f014ab7cdd to your computer and use it in GitHub Desktop.
Component to delay rendering
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