Created
October 20, 2020 20:14
-
-
Save iosorin/672fe7aa6583ebb2872425df29418ab8 to your computer and use it in GitHub Desktop.
GuardedRoute.tsx
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 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