Skip to content

Instantly share code, notes, and snippets.

@iosorin
Created October 20, 2020 20:14
Show Gist options
  • Select an option

  • Save iosorin/672fe7aa6583ebb2872425df29418ab8 to your computer and use it in GitHub Desktop.

Select an option

Save iosorin/672fe7aa6583ebb2872425df29418ab8 to your computer and use it in GitHub Desktop.
GuardedRoute.tsx
import React, { ComponentType, FC } from 'react';
import { Redirect, Route, RouteProps } from 'react-router-dom';
type Props = RouteProps & {
component: ComponentType;
redirect?: string;
show?: boolean;
};
const GuardedRoute: FC<Props> = ({ component: Component, redirect = '/', show = false, ...rest }) => {
return (
<Route render={(props) => (show === true ? <Component {...props} /> : <Redirect to={redirect} />)} {...rest} />
);
};
export default GuardedRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment