Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
my code inside my PrivateRoute component:
import React, { useContext } from 'react';
import { Redirect, Route } from 'react-router';
import { UserContext } from '../App';
const PrivateRoute = ({ children, ...rest }) => {
const [loggedInUser, setLoggedInUser] = useContext(UserContext);
return (
<Route
{...rest}
render={({ location }) =>
loggedInUser.email ? (
children
) : (
<Redirect
to={{
pathname: "/login",
state: { from: location }
}}
/>
)
}
/>
);
};
export default PrivateRoute;