Skip to content

Instantly share code, notes, and snippets.

@pubudu-ranasinghe
Created November 3, 2019 13:07
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
react-context-example-20
import React from "react";
import { Route, Redirect } from "react-router-dom";
import { useAuthContext } from "../contexts/AuthContext";
export default function PrivateRoute({ children, ...rest }) {
const { auth } = useAuthContext();
return (
<Route
{...rest}
render={({ location }) =>
auth.isLoggedIn ? (
children
) : (
<Redirect to={{ pathname: "/", state: { from: location } }} />
)
}
/>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment