Templates vs Layouts in NextJs 13
Ғылым және технология
This video will examine two particular files in NextJs 13's App router: template and layout. We'll discuss their differences and when to use each in a simple application.
👉🏼 The Ultimate NextJs Course
🔗 www.hamedbahram.io/courses/ne...
👉🏼 Project source code (Github)
🔗 github.com/HamedBahram/next-r...
👉🏼 Work with me
🔗 www.hamedbahram.io/hire
Chapters:
0:00 Intro
3:55 Project setup
6:20 Logging page views
9:30 Adding template
12:17 Adding layout animation
16:00 Recap
Пікірлер: 83
I absolutely love your content Nextjs is easily one of the most in demand tech currently and you're providing one of most clear and concise resources for learning it all the way from the basics to the more advanced topics I hope your channel continues to grow, you deserve every success
@hamedbahram
8 ай бұрын
Thanks Kyle! I appreciate your comment, and I'm glad you're finding the videos helpful.
Wonderful lesson thank you. I'm reading throught the NextJS docs now and am so glad you cleared up the difference between layouts and templates.
@hamedbahram
7 ай бұрын
My pleasure! Glad it was helpful.
Thank you so much for your support and teaching, I'm really understanding your explanations.
@hamedbahram
8 ай бұрын
You are very welcome. Glad to hear that!
Thank you Hamed, It's been nothing but Imppactful and Growth since i subscribed to your channel
@hamedbahram
5 ай бұрын
My pleasure! Glad to hear that.
Thank you Hamed, This video makes a lot of sense to understand between layout and template.
@hamedbahram
4 ай бұрын
Glad it was helpful!
congratulation for reaching 10k subs well deserving, your content is really great explaing all the bits and pieces of front end development
@hamedbahram
8 ай бұрын
Thanks! I appreciate it.
Again top notch content on NextJS Thank you, keep it up 👍🏻
@hamedbahram
8 ай бұрын
My pleasure!
Clear and understandable examples. Thank you!
@hamedbahram
5 ай бұрын
Glad it was helpful!
Thank you Hamed. I appreciate your fantastic tutorial
@hamedbahram
8 ай бұрын
Anytime Hasan! I'm glad it was helpful.
congrats bro for the 10K, keep rocking :)
@hamedbahram
8 ай бұрын
Thank you so much 😀
Such a great explanation, Well done Hamed👏
@hamedbahram
7 ай бұрын
Thank you! 🫡
Nice explanation, so if you have similar code in two different pages that share the same parent folder, you can extract that code into a common template and put it in the parent folder. It can also provide a client wrapping mechanism to your nested server pages!
@hamedbahram
7 ай бұрын
Yes, exactly! You can accomplish that with both `layout` and `template`. The difference is `layout` doesn't re-render on page navigations whereas `template` does.
we would love a framer motion video with nextjs
@hamedbahram
6 ай бұрын
Absolutely! That has been overdue. Will work on it.
I thought it just like layout but this video is awesome. Got to know more about template' functionality
@hamedbahram
5 ай бұрын
Glad it was helpful!
Loving your videos, great content and well explained. Have a question, if you convert the template or the layout into a client component will all the childrens be client components too?
@hamedbahram
8 ай бұрын
Thank! Glad to hear that. Re your question: only if you import a server component inside a client component will they turn into client components. You can pass server components as children as I did in the video without effecting the server-client boundary. Let me know if that's clear.
Great tutorial ❤
@hamedbahram
8 ай бұрын
Thanks! Glad you found it helpful.
beautifully explained
@hamedbahram
2 ай бұрын
Thank you! 🙂
Best regards from Egypt
@hamedbahram
8 ай бұрын
Welcome to the channel!
Thanks buddy 😊
@hamedbahram
8 ай бұрын
Any time!
Hi really loved video. By the way what is the font u are using for VS Code editor
@hamedbahram
5 ай бұрын
Thanks! I'm using Operator mono.
This is Gold
@hamedbahram
7 ай бұрын
Thanks 🫡
Thanks
@hamedbahram
8 ай бұрын
Anytime!
thank you
@hamedbahram
7 ай бұрын
You're welcome
so , we should use template to add exit route animations i tried it but it dosen't work what key should i pass to AnimatePresence and Thanks hamed for your amazing content
@hamedbahram
8 ай бұрын
Yeah that should work, you can use any unique random key or the current path as the key.
🇳🇬 Great job
@hamedbahram
7 ай бұрын
Thanks!
Could you u use dark theme in the browser as well? 😊
@hamedbahram
8 ай бұрын
I'll try it next time 🙂
Hi, thanks for the video. I was trying to use template "server side" but unfortunatly when you navigate with LInk component, the template it seems no refresh some other ""server side" component inside. Why? Thanks for the answer.
@hamedbahram
4 ай бұрын
The template itself would re-render on every navigation but the server components inside are cached by default. There are different ways to opt your page components into dynamic rendering, but the easiest way is to use the `noStore` function. You can read about it here → nextjs.org/docs/app/api-reference/functions/unstable_noStore
@LorenzoSemorile
4 ай бұрын
@@hamedbahram thanks a lot. I will try tomorrow and i will let you know. Sorry I’m learning nextjs framework in theese days. Thanks for your videos.
@hamedbahram
4 ай бұрын
@@LorenzoSemorile Glad to help!
i have a login page (Auth pages) how can i remove the components in RootLayout in those pages
@hamedbahram
3 ай бұрын
You can use route groups to create to different layout. Watch this → kzread.info/dash/bejne/eaNo1K5qZcq_hNY.html
So what's the purpose of having a layout with a "use client"? Anytime someone wants to use "use client' he can right away use a template instead. And also, why would someone use template without 'use client"? Any case will always use "use client".? I'm a bit confused.
@hamedbahram
6 ай бұрын
Typically your layout is a server component that gets rendered once and remains the same when user navigates. The template on the hand will re-render every time because it uses a `key` prop. The template doesn't need to be a client component necessarily. I turned it into a client component in this example because I was using framer motion, but you don't need to.
Does the template force the component to re-render?
@hamedbahram
6 ай бұрын
Yes exactly.
The mouse pointer in the video is not visible
@hamedbahram
6 ай бұрын
Yeah when I switch screens the mouse hides. I'll have to remember to click on tabs before pointing at different things on the page. Thanks for the feedback.
how to pass data we fetched in layout to children pages
@hamedbahram
4 ай бұрын
Not a straight forward way to do that, but you don't need to pass props, just re-fetch your data on the page as well, and React will de-duplicate the request.
@AllahomAnsorGaza
4 ай бұрын
you are great teacher ☺️ thanks for all of your efforts
@hamedbahram
4 ай бұрын
@@AllahomAnsorGaza Thank you! I appreciate that.
But if I want to hide the header for about page?
@hamedbahram
7 ай бұрын
Then don't put it in that route group.
@tuRistst
7 ай бұрын
Thanks, but I found a solution using Route Groups
your tutorial is excellent but i think content like this can be shorten to at most 10 mins
@hamedbahram
6 ай бұрын
Thanks for your suggestion.
I hope fixing exit animations on Templates is high on their roadmap, forced to use pages router for page animations
@hamedbahram
8 ай бұрын
Hmm 🤔 didn't know there is a problem with exit navigation! I'll give it a try. Thanks for sharing.
@SanderCokart
8 ай бұрын
@@hamedbahram templates document the use of exit animations but framer motions Animate Presence doesn't work with this because page is not the direct ancestor of the template
@hamedbahram
8 ай бұрын
@@SanderCokart That's right. I just tested this, and while the exit navigation doesn't run, you can still animate the page. I changed the `main` tag that wraps the page in my template to `motion.main`
@SanderCokart
8 ай бұрын
@@hamedbahram yes only initial and animate. Pages router is required till then.
You are *"Jeff Goldblum"* right? Am I the only one who thinks that?
@hamedbahram
6 ай бұрын
I'm not that old 😅
not good explanation
@hamedbahram
2 ай бұрын
Thanks!