Last active
March 30, 2019 11:43
-
-
Save mathiassoeholm/0ca98424cd2c413a3fa045c3ef0ec251 to your computer and use it in GitHub Desktop.
DelayedComponent for React
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
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