Created
October 17, 2020 09:53
-
-
Save JT501/4b0f60fb57b1410604b754fd9031150a to your computer and use it in GitHub Desktop.
React Router - DelayRedirect
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'; | |
import { useEffect, useState } from 'react'; | |
import { Redirect, RedirectProps } from 'react-router'; | |
interface DelayProps { | |
delay: number; | |
} | |
const DelayRedirect = ({ delay, ...rest }: RedirectProps & DelayProps) => { | |
const [timeToRedirect, setTimeToRedirect] = useState(false); | |
useEffect(() => { | |
const timeout = setTimeout(() => { | |
setTimeToRedirect(true); | |
}, delay); | |
return () => clearTimeout(timeout); | |
}, [delay]); | |
return timeToRedirect ? <Redirect {...rest} /> : null; | |
}; | |
export default DelayRedirect; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: