Last active
July 13, 2020 03:17
-
-
Save GGAlanSmithee/a5883c098847bebcd5092f3faa30b536 to your computer and use it in GitHub Desktop.
react-router DelayedRedirect
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 * as React from 'react' | |
import { Redirect, RedirectProps } from 'react-router' | |
interface DelayedProps { | |
delay: number | |
} | |
interface DelayedState { | |
timeToRedirect: boolean | |
} | |
class DelayedRedirect extends React.Component< | |
RedirectProps & DelayedProps, | |
DelayedState | |
> { | |
timeout: any = null | |
state: DelayedState = { | |
timeToRedirect: false, | |
} | |
componentDidMount() { | |
this.timeout = setTimeout(() => { | |
this.setState({ | |
timeToRedirect: true, | |
}) | |
}, this.props.delay) | |
} | |
componentWillUnmount() { | |
clearTimeout(this.timeout) | |
} | |
render() { | |
const { delay, ...props } = this.props | |
const { timeToRedirect } = this.state | |
if (timeToRedirect) { | |
return <Redirect {...props} /> | |
} | |
return null | |
} | |
} | |
export default DelayedRedirect |
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 * as React from 'react' | |
import { DelayedRedirect } from './delayed-redirect' | |
const Test = () => ( | |
<div> | |
Redirect to "/" in fem seconds | |
<DelayedRedirect to={'/'} delay={5000} /> | |
</div> | |
) | |
export default Test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See remix-run/react-router#6422 for context