In this lesson we'll create a protected route just for logged in users. We'll combine a Route with a render prop and use a loggedIn prop to determine if the route should be allowed to be accessed. Finally we'll use nav state to preserve the location the user visited and redirect them back to the protected route once they login.
1. Create a ProtectedRoute is nothing but just a react component render a Route component:
check the 'loggedIn' props, if true, then using render prop to render the component normally.
If 'loggedIn' props is false, then use 'Redirect' component to redirect to Home page. also pass the route state.
2. When the login button is clicked, we want to force udpate the state using callback in setState:
<code>setState()</code> does not always immediately update the component. It may batch or defer the update until later. This makes reading <code>this.state</code> right after calling <code>setState()</code> a potential pitfall. Instead, use <code>componentDidUpdate</code> or a <code>setState</code> callback (<code>setState(updater, callback)</code>), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the <code>updater</code> argument below. Link