Gatsby Tutorial #4 - Navbar & Links

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Creates a navbar component with Gatsby and uses the Link component to link to each page

Full Transcript

all right then gang so now i have a few different pages it would be nice to create a navbar so we can link between those pages on the page and by the way i've deleted the test file inside projects because we don't need that anymore now just one quick thing before we go on when you're working with pages if you sometimes delete them or move them around you might get an error from gatsby in the browser now if you get that don't worry all you have to do is cancel out of the process down here where we run gatsby develop and you can do that by clicking control c that's going to cancel out of that then you can run gatsby develop again that's going to restart the server for us and hopefully then the error will go away all right then so what i'm going to do is create a new navbar component so where do we create this component well we don't create it inside the pages folder because that wouldn't make any sense the navbar is not going to be a page in itself it's just going to be a component that we can drop into other pages so what i'm going to do instead is create a new folder inside the source folder right here so new folder and i'm going to call this components now you don't have to call it components you can call it whatever you want but i'm going to keep my other non-page components that can be dropped into pages right here so inside here i create a new file called navbar now typically when i'm working with gatsby i generally start these with a capital letter these dropping components the pages down here i don't use capitals but the dropping components i do so i'm going to call this navbar.js this will be a react functional component called navbar okay then so i want to keep this really simple first of all i'm going to change the div to a nav and then inside here i'm going to do an h1 that says web oops let me create that tag first of all it's going to say web warrior cheesy name i know and then down here i'm going to do a div i'm going to give this a class name of links and that's just so that i want to style this later on i can do all right then so now i want several different links so you might think i use anchor tags but no we don't do that much like we don't do that in react applications we use a special component called a link component instead and that's the same as just using react on its own without gatsby that link component means that all the routing is handled in the browser only and we don't send extra requests for new pages to the server because if we didn't use that link component this would actually send a request to the server we don't want this to happen because when we get that initial page we want our application to behave more like a single page application where all routing is done in the browser so instead of this we need to do a link tag and what i'm going to do is just click on that and it should auto import it at the top so we import link from gatsby all right then so this thing right here has a prop called two and that is basically the href what route do we want to go to when someone clicks on this link now the first one is going to be forward slash and that is going to be just for the home page so let me duplicate this a couple of times the second one is going to be to forward slash about and that's for this about page so let's change the text to about and then finally i'm going to go to forward slash projects which remember is going to be this index page inside the project folder and right here i'm going to call this port folio projects like so if i can spell it that is all right so we have this component now and all we have to do is now nest this component into our pages now i'm not going to do it in all of them to begin with i'm just going to go to the index file over here and i'm going to nest it at the top of this file so first of all let me import it at the top so import navbar and it's going to be from then we need to come out of this current pages folder first so dot dot forward slash then into components then we want the navbar file all right so now we can nest that inside here nav bar like so okay then and again if you're struggling with any of this react code definitely check out my react series first because this is all pretty basic react stuff all right so now hopefully we should see this navbar content at the top of the homepage let me try this out yeah we can see it right here and if we click on one of these for example portfolio projects we see that page and it all happened really quickly because all the routing is happening in the browser now if i go to about we see the about page obviously we're already on the home page so that does nothing so that is our links done and that is our navbar done now what we could do is we could take this nav bar and we could place it at the top of every different file and then also maybe if we wanted a footer component we could create one of those and we could place that at the bottom of every different file and then the same for a sidebar if we had one of those we create a sidebar component and we'd nest that in each of these different files as well but it all gets a little bit too complex and hard to update if we're nesting three or four different layout components things that don't change over each page in our pages instead it would be nice to create a single layout component which incorporates the navbar the footer and anything else that's consistent across all pages and then just wrap all of our different pages right here with that single layout component and we're going to see how to do that next

Original Description

Hey gang, in this Gatsby tutorial we'll create a navbar component to sit at the top of each page & also use the Link component to link to each page. 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/gatsby-tutorial 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS 3 & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Useful playlists: + Full React tutorial - https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d + GraphQL tutorial - https://www.youtube.com/watch?v=Y0lDGjwRYKw&list=PL4cUxeGkcC9iK6Qhn-QLcXCXPQUov1U7f 🐱‍💻 🐱‍💻 Other links: + Gatsby docs - https://www.gatsbyjs.com/docs/ + Markdown guide - https://guides.github.com/features/mastering-markdown/ + Download Git - https://git-scm.com/downloads + Get VS Code - https://code.visualstudio.com/ 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
Automating Asset Sourcing for Frontend Projects: A Practical Workflow
Automate asset sourcing for frontend projects to save time and increase efficiency
Dev.to · Naveed Javaid
📰
I Tried Vite+ Beta: Can One Tool Replace Your Frontend Toolchain?
Learn how Vite+ Beta can simplify your frontend workflow by combining development, builds, tests, and more into one tool, and why it matters for efficient development
Dev.to · 𝗝𝗼𝗵𝗻
📰
Angular vs React: Key Differences, Pros & Cons Compared
Learn the key differences between Angular and React to decide which framework suits your project needs, considering factors like team size, project complexity, and desired level of flexibility.
Dev.to · Elsie Rainee
📰
Next.js Web App Development: Complete Guide for Fast, Scalable Applications (2026)
Learn how to build fast and scalable web applications using Next.js, a popular React framework, to improve user experience and conversion rates
Dev.to · Avanexa Technologies
Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →