Next.js Tutorial #5 - Creating a Layout Component
Skills:
React90%
Key Takeaways
Creates a layout component in Next.js to wrap page content using a stateless functional component
Full Transcript
all right then so we've seen how we can create these dropping components the navbar and the footer which currently sit at the top and the bottom of the homepage now ideally i'd like these to go at the top and the bottom of every single page that we have so i guess what i could do is copy this and copy this and paste them at the top and bottom of every single page components but that's not really realistic especially for larger websites where we have many different pages because then i'd have to copy and paste in every single page component that we create which is a waste of time and also if the layout changes for example where the navbar is shown or where the footer is shown i'd have to change that in every single page component so ideally what i'd like to do is create instead a layout file and that layout file would contain the nav bar in the footer and then that component that layout component would wrap all of our different pages so in essence what we need to do is create that layout component and have it wrap this thing right here because this is where our pages are rendered okay so what i'll do is first of all create the layout component inside the comps folder so layout.js and then we'll create a stateless functional component called layout right here and what we'll do now is first of all go to app.js and i'm going to import it and i'm going to wrap this thing with the layout components so let me just cut this for a second and instead we'll return a bit of template inside parentheses where we're going to have the layout first so let me click on this to auto import it at the top make sure this is an angle bracket not a comma all right and we need this to have a closing tag so that in between these components we can place this thing right here this is the thing that renders the pages and what we're doing is wrapping the layout component around that now we get access to whatever is nested inside the layout so this thing right here inside the component that surrounds it so the layout itself and this is called the children of this component this is standard react stuff so if we go in here now inside this layout component on the props we get access to a children property which i'm just destructing right here so this children property is basically anything inside this layout component right here anything nested inside it so it's just this and all we need to do now let me save this file is output the children inside this component and what that means is that wherever we output the children inside this component all the pages are going to go so what we could do is output the nav at the top the footer at the bottom and then the children in the middle so let me first of all do a div with a class name of content just so we can style this later on and then inside that i'm going to do the nav bar let me click on that to auto import it at the top so we have that first of all then i'm going to output the children which is all the pages all the page content and then below that we need the footer i'm going to click on this and it auto imports it and now this should all work so what i could do is delete it from here and delete it from here but it's still going to appear let me test this out first of all go to the home page i'm going to refresh for good measure and it's still there we have the nav bar and also the footer now though if i click on a different page like about we have it here as well the nav bar and the footer if we go to ninja listing we have it there as well navbar footer so now it's appearing at the top and the bottom of every single page component so again that works because we surrounded all of our different components with this layout file inside app.js so we nested this thing which is where the pages are output inside the layout components therefore inside the layout components we output this this thing right here by referencing the children because the children is this thing it's the children of the layout components and so therefore the pages are nested between the navbar and the footer does that make sense so now whatever page we create it's going to have the nav bar at the top and the footer at the bottom
Original Description
Hey gang, in this Next.js tutorial we'll see how to reate a layout component (navbar and footer) which can wrap our page content.
🐱💻 🐱💻 Course Files:
+ https://github.com/iamshaunjp/nextjs-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 & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Helpful Links:
+ Full React Course - https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d
+ HTML & CSS Course - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G
+ Modern JavaScript course - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
+ Get VS Code - https://code.visualstudio.com/
+ Next.js docs - https://nextjs.org/docs/getting-started
🐱💻 🐱💻 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: React
View skill →Related Reads
📰
📰
📰
📰
The Enter key that submits your form while a Japanese user is still typing
Dev.to · greymoth
The two-Reacts bug: when packages aren't singletons
Dev.to · r9v
🚀 Introducing Prism Guard — An Open Source Frontend Architecture Intelligence Platform
Dev.to · Ritumoni Sarma
The Death of the Heavy Hydration Layer
Dev.to · Amodit Jha
🎓
Tutor Explanation
DeepCamp AI