I'm fairly new to React Router. I'm trying to implement an Auth check when the app runs as answered here: How to implement authenticated routes in React Router 4?. If logged in it should load the dashboard component, if not you should get redirected to the login screen.
The redirect does not work, it's just a blank page.
Any ideas?
export default class App extends React.Component {
state = {
auth: false
};
updateAuth = value => {
this.setState({
auth: value
});
};
render() {
const { auth } = this.state;
console.log(auth);
const PrivateRoute = ({ component: Component, ...rest }) => {
return (
<Route
{...rest}
render={props =>
auth ? (
<Component {...rest} />
) : (
<Redirect
to={{ pathname: "/login", state: { from: props.location } }}
/>
)
}
/>
);
};
return (
<Router>
<div>
<Route path="/login" component={Login} auth={this.updateAuth} />
<PrivateRoute path="/dashboard" component={Dashboard} />
</div>
</Router>
);
}
}