redirect.tsx 591 B

123456789101112131415161718192021222324252627282930
  1. import {useEffect} from 'react';
  2. import {InjectedRouter} from 'react-router';
  3. import {useNavigate} from './useNavigate';
  4. type Props = {
  5. to: string;
  6. router?: InjectedRouter;
  7. };
  8. /**
  9. * Like react-router v4+'s <Redirect to="path/" />, this component allows
  10. * redirects to be declarative.
  11. */
  12. function Redirect({to, router}: Props) {
  13. const navigate = useNavigate();
  14. // Redirect on mount.
  15. useEffect(() => {
  16. if (router) {
  17. router.replace(to);
  18. } else {
  19. navigate(to, {replace: true});
  20. }
  21. }, [navigate, router, to]);
  22. return null;
  23. }
  24. export default Redirect;