Skip to content

Instantly share code, notes, and snippets.

@JT501
Created October 17, 2020 09:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JT501/4b0f60fb57b1410604b754fd9031150a to your computer and use it in GitHub Desktop.
Save JT501/4b0f60fb57b1410604b754fd9031150a to your computer and use it in GitHub Desktop.
React Router - DelayRedirect
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;
@JT501
Copy link
Author

JT501 commented Oct 17, 2020

Usage:

<DelayRedirect to={'/'} delay={1000} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment