Skip to content

Instantly share code, notes, and snippets.

@pubudu-ranasinghe
Created November 3, 2019 13:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pubudu-ranasinghe/fb9e5996bc948b21a0e6e004f800983c to your computer and use it in GitHub Desktop.
Save pubudu-ranasinghe/fb9e5996bc948b21a0e6e004f800983c to your computer and use it in GitHub Desktop.
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