Skip to content

Instantly share code, notes, and snippets.

@mathiassoeholm
Last active March 30, 2019 11:43
Show Gist options
  • Save mathiassoeholm/0ca98424cd2c413a3fa045c3ef0ec251 to your computer and use it in GitHub Desktop.
Save mathiassoeholm/0ca98424cd2c413a3fa045c3ef0ec251 to your computer and use it in GitHub Desktop.
DelayedComponent for React
import React, { Component, ReactNode } from "react"
interface Props {
delayMs: number,
children: ReactNode,
}
interface State {
enabled: boolean,
}
class DelayedComponent extends Component<Props, State> {
state: State = {
enabled: false,
}
constructor(props: Props) {
super(props);
this.componentDidMount = this.componentDidMount.bind(this);
}
componentDidMount(): void {
setTimeout(() => {
this.setState({enabled: true})
}, this.props.delayMs);
}
render() {
return this.state.enabled && this.props.children;
}
}
export default DelayedComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment