How to Build a React Header With a React Log Out Mutation

A user is now able to successfully sign-in from the Login page. In this lesson, we'll spend some time creating the AppHeader component that will help allow users to navigate around our app.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two

We've been able to go through the login flow and essentially log in a user and have the viewer details available on the client app. In this lesson we'll look to notify the user whether they're in the logged in or logged out state. And we'll do this within an app header that will also help the user with navigating between different routes. We'll create an app header component that will be shown in all different pages and will allow the user to navigate around the app, log in and log out. We'll have this app header component be established within a new sections folder. In this app header folder we've also just added an assets that references our application logo. We'll provide this as part of the lesson documentation and source code. The app header components will use the layout header from and design and simply display the logo for now. (Silence) (Silence) Will import and use the link components from React Router to allow the user to click the logo and actually navigate back to the index route depending where they may be in the app. In the parent index file, we'll import the app header component and look to place it at the top of our UI. To have the app header component shown in every route, we'll place it outside of the Router switch statement. To ensure the app header component stays affixed to the top even if we were to scroll down in a page, we'll import and use the affixed components from end design and wrap our app header component with it. By specifying the offset top option to zero, we're stating that we want the child component to stay at the very top of the page. We're now presented with the app header that shows our application logo regardless of what route we're in. We'll now leverage the menu components from end design to help show a menu of links in the app header that the user can use. When the user is signed off, we want to show a link to the host page and a sign -in button to take them to the login page. However, when the user is signed in, instead of the sign-in button, we're interested in showing the avatar of the user that also acts as a drop-down button that allows the viewer to navigate to their profile, or that is to say their user page, or log out of the application . Since this menu item section is to have some decent functionality, we'll abstract this way to a new component we'll call menu items that will set up in the components folder of app header. [silence] At first, we'll aim to have menu items, show the host menu item, and the sign- in button. We'll import and use the button icon and menu components from end design to help us here. We'll destruct the child item and submenu components from end design that we'll use. [silence] [silence] [silence] And we'll use the link component from React Router to have the host item be a link to the slash host route. [silence] And the sign-in button be a link to the slash login route. [silence] We'll now import the menu items component in the app header component and render it as a child of app header. [silence] If we take a look at our UI, we can see the link's being shown, but there's a slight placement issue. If we take a look at our menu items component, we should move the text for host within the link element itself. [silence] When we now click the host link or the sign-in link, we'll be taken to the host page and login page respectfully. We'll now want to conditionally show the viewer profile image if the user is logged in or the sign-in button when the user, that is to say viewer, is logged out. To do so, we'll need access to the viewer state value in our parent app component. In the parent app component, we'll pass the viewer state value as a prop down to the app header component. In the app header component, we'll declare the viewer prop and pass it along down to the menu items child component. We'll import the viewer interface from the lib types file and set the type of the viewer prop to this viewer interface. [silence] [silence] [silence] [silence] With the viewer state value available in the menu items component, we can now conditionally render the drop-down menu or a button element. The drop-down menu will be created with the sub-menu child component and design provides. We'll assign it to a constant element called sub-menu login. This sub-menu login element will have the menu items called profile which will be used as an eventual link to the user page and a log-out item which will be eventually a trigger to help log out to the viewer. We'll conditionally have the sub-menu login element display the sub-menu item if the viewer is logged in. To do so, we can check for a valid property of the viewer object. For example, if the ID of the viewer exists, it most likely means the server was able to return a viewer instance. If the viewer doesn't exist, we'll have the sub-menu login element display the sign-in button and will render this sub-menu login constant element in the return statement of the menu items component. The sub-menu component takes a title prop with which we're able to use to show the avatar of the logged in user. We'll import an avatar component from end design and place it in the title prop and use the avatar property within the viewer object as the source. TypeScript emits a warning and rightfully so since the viewer interface avatar field may be null in certain conditions. However, when the viewer ID exists, we know the viewer avatar field will also exist. So we'll state that both the viewer ID and viewer avatar fields should exist and that is to say not be null to render the sub-menu element. Let's observe what we've done so far. In our UI, we'll log in and go through the consent flow. When being taken back to our app, we'll notice after a very brief period, we're now presented with our avatar image and a drop-down menu. Amazing. As we can see, both items in the drop-down say profile, so we'll go ahead and correct the bottom item to say log out instead. Notice something interesting? When our app refreshed after our change, our login state in the client is gone. That's unfortunate and doesn't really allow the user to maneuver around since they have to avoid refreshing the page. This is going to be the investigation of the next module of the course. For now, we'll re-login and verify that our drop-down log-out action says "Log Out". Great. We'll now wire the log-out mutation with the log-out button in our app header. As a note, if we take a look at the log-out mutation resolver function in the server, it actually doesn't do much right now. It just returns an empty viewer object, something we can just do on the client and set it on the viewer state value. In the next lesson, log-out, or the log-out mutation will help clear out a cookie to prevent the user from having a persistent login state. Though it won't have much of a functionality now, we'll have the log-out item in the drop-down trigger the log-out mutation to set us up for the next module. In the menu item's component, we'll import the log-out mutation document and the auto-generated interface for the data to be returned from the mutation. And we'll also import the use mutation hook. At the top of the menu item's component function, we'll use the use mutation hook and simply return the log-out mutation request function only. In the sub-menu login element, we'll wrap the contents of the log-out item with a div element that when clicked will trigger the component handle log-out function that will be returned from the function. In the log-out function that will subsequently call the log-out mutation function. We're using a div element as to avoid changing the styling of this item, and we 're having a component function call the request since the request is a promise. We'll want to attach a standard function to the click listener that then calls the promise. And in the profile item above, we'll use the link component from React Router to help navigate the viewer to the user page with the respective ID of the viewer. When the log-out mutation is successful, we'll want to notify the user that it was successful and set the viewer client state object in the parent to a value that references a signed-off user. To update the viewer state value, we'll need access to the setViewer function available in the parent app component. In the parent app component, we'll pass the setViewer function as a prop down to app header. In the app header component, we'll declare the prop and pass it along to the menu item's component. In the menu item's components, we'll declare the setViewer function as an expected prop as well. When the log-out mutation is successful, it will return the viewer object type in the offline state, in which all of the values in the object is set to null, and the did request field is set to true. And we'll have this as part of the request data. So we'll use the mutation on completed callback and run the setViewer function to set the viewer state value with the new received viewer objects. We'll also look to display a success notification to tell the user that they were able to sign off successfully. We'll import the display success notification function we have in the libutills file and run it after the setViewer function. If the mutation was to ever fail, we'll want to notify the user this. When the mutation fails in this case, we won't want to render a banner of source or anything in the UI. Instead, we'll want to run the display error message function we have in our libutills file to show an error alert. Since we want to simply run a function, we can do this in the onError callback of the mutation, which is a callback function reactor polar provides that runs when the mutation has failed. Okay, that should help us achieve what we intend to achieve. If we take a look at our app, sign in again and click the logout menu item, we 'll notice that the viewer state has been removed from our clients. Great! Only other change we'll do right now is remove the console log we had in the parent app component that logged in the viewer object. Now, once again, the logout graphQL mutation at this moment doesn't really achieve anything significant. It simply returns an empty viewer object. We could have just created an empty viewer object and set it on the client side of our state viewer value instead. In the next module, we're going to see how the logout mutation will help clear a persistent login state of the user.