Skip to content

Instantly share code, notes, and snippets.

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

  • Save iosorin/98a13a4d58825ded18810670f0c142bc to your computer and use it in GitHub Desktop.

Select an option

Save iosorin/98a13a4d58825ded18810670f0c142bc to your computer and use it in GitHub Desktop.
PrivateRoute, react-ts
import React, { ComponentType, FC } from 'react';
import { useSelector } from 'react-redux';
import { Redirect, Route, RouteProps } from 'react-router-dom';
import { getAuthenticated } from '@/store/selectors';
type PrivateRouteProps = RouteProps & {
component: ComponentType;
};
const PrivateRoute: FC<PrivateRouteProps> = ({ component: Component, ...rest }) => {
const isAuthenticated = useSelector(getAuthenticated);
return (
<Route
render={(props) => (isAuthenticated === true ? <Component {...props} /> : <Redirect to="/" />)}
{...rest}
/>
);
};
export default PrivateRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment