I am using a higher order component (PublicRoute) to conditionally render another component depending on whether the user is authenticated or not. I have made another higher order component, PrivateRoute, to achieve the opposite effect. The idea is that I don't want users logged in to be able to access the login page, and I don't want users who aren't logged in to view other pages. (Pragmatically speaking, this is unlikely, unless they manually type in the URL, but still, I want to ensure that it doesn't happen.)
The problem is that the Redirect component is only working after the Login component renders for a few seconds. This seems like bizarre behavior. The auth piece of the state contains either an object or is false or null (null is the default state). So there shouldn't be any problem. The same problem occurs with the PrivateRoute (e.g. an unauthenticated user can still access the Cart page).
PublicRoute.js
export const PublicRoute = ({
isAuthenticated,
component: Component,
...rest
}) => (
<Route
{...rest}
component={(props) => (!isAuthenticated ? (
<div>
<Component {...props}/>
</div>
) : ( <Redirect to="/shop"/> )
)}/>
);
const mapStateToProps = ({ auth }) => ({ isAuthenticated: !!auth });
export default connect(mapStateToProps)(PublicRoute);
AppRouter.js
<PublicRoute path="/login" component={Login}/>