Making Decisions About State



Now, it’s time to log out of the application. Do that with the following steps.

The steps that the application will take are these:

  1. Someone clicks the logout button
  2. The LogoutButton component dispatches a thunk
  3. The thunk makes the AJAX call to logout
  4. If that AJAX call succeeds, remove the token from local storage and dispatch an action to remove the token from the store
  5. Redux will invoke a reducer that removes the token from the store
  6. The LogoutButton will redirect the application back to “/login”

In src/store/authentication.js:

In src/LogoutButton.js:

(If you make a mistake with this and get into an inconsistent state, just delete all of the contents of your local storage and refresh your browser.)

If you check the console, now, you’ll see that Babel is reporting a “useless” constructor. Sure enough, it is. LogoutButton no longer has any state, so there’s no reason to leave it as a class-based component. Convert it to a function-based component. If you’ve followed these instructions, your LogoutButton should end up looking something like this.

const LogoutButton = props =>
  props.loggedOut ?
    <Redirect to="/login" /> :
    <div id="logout-button-holder">
      <button onClick={props.logout}>Logout</button>
    </div>
;