Skip to content

Instantly share code, notes, and snippets.

@ashug0001
Created December 16, 2020 16:11
Show Gist options
  • Save ashug0001/dee1e6d19ed7e93da10e46d75193a7d2 to your computer and use it in GitHub Desktop.
Save ashug0001/dee1e6d19ed7e93da10e46d75193a7d2 to your computer and use it in GitHub Desktop.
Protected Route for React for Redux
import React from "react";
import { Route, Redirect } from "react-router-dom";
import { connect } from "react-redux";
import PropType from "prop-types";
const ProtectedRoutes = ({ isLoggedIn, children, ...rest }) => {
return isLoggedIn ? (
<Route {...rest} render={(props) => children} />
) : (
<Redirect to="/login" />
);
};
ProtectedRoutes.propType = {
isLoggedIn: PropType.bool.isRequired,
};
const mapState = (state) => {
const isLoggedIn = state.auth.isLoggedIn;
return {
isLoggedIn,
};
};
export default connect(null, null)(ProtectedRoutes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment