Frontend Serverless with React and GraphQL
Start at the beginning and work your way through this project. The code for each step as well as the finished project can be found in the Github repository.
23. Add Login / Logout to the Navbar
Objective: Add user login and logout button to the site's navbar.
First, let's do a quick housekeeping. Add @ts-ignore
to the line before export function useFetchUser({ required } = {}) {
to prevent it from complaining in the utils/user.ts
file we just created.
Now, we will now use the new useFetchUser
hook that we just created to selectively show either the login or the logout button in the navbar, based on whether the user has logged in.
Note that for the ternary user ? [ <Component/> ] : [ <Component /> ]
we use brackets instead of paretheses because we plan to add additional menu items into these conditionals and we want to set it up as returning an array of components rather than a single component right from the start.