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

  • @kyle-andrewgovinder9902
    @kyle-andrewgovinder99028 ай бұрын

    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

    @hamedbahram

    8 ай бұрын

    Thanks Kyle! I appreciate your comment, and I'm glad you're finding the videos helpful.

  • @andrew_schaeffer
    @andrew_schaeffer7 ай бұрын

    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

    @hamedbahram

    7 ай бұрын

    My pleasure! Glad it was helpful.

  • @devmab
    @devmab8 ай бұрын

    Thank you so much for your support and teaching, I'm really understanding your explanations.

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    You are very welcome. Glad to hear that!

  • @samuelizevbizua
    @samuelizevbizua5 ай бұрын

    Thank you Hamed, It's been nothing but Imppactful and Growth since i subscribed to your channel

  • @hamedbahram

    @hamedbahram

    5 ай бұрын

    My pleasure! Glad to hear that.

  • @uzairahmed2975
    @uzairahmed29754 ай бұрын

    Thank you Hamed, This video makes a lot of sense to understand between layout and template.

  • @hamedbahram

    @hamedbahram

    4 ай бұрын

    Glad it was helpful!

  • @Gangbuster74
    @Gangbuster748 ай бұрын

    congratulation for reaching 10k subs well deserving, your content is really great explaing all the bits and pieces of front end development

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    Thanks! I appreciate it.

  • @rohitghodeswar11
    @rohitghodeswar118 ай бұрын

    Again top notch content on NextJS Thank you, keep it up 👍🏻

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    My pleasure!

  • @albert21994
    @albert219945 ай бұрын

    Clear and understandable examples. Thank you!

  • @hamedbahram

    @hamedbahram

    5 ай бұрын

    Glad it was helpful!

  • @hasancheraghi2800
    @hasancheraghi28008 ай бұрын

    Thank you Hamed. I appreciate your fantastic tutorial

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    Anytime Hasan! I'm glad it was helpful.

  • @anversadutt
    @anversadutt8 ай бұрын

    congrats bro for the 10K, keep rocking :)

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    Thank you so much 😀

  • @afshinkaramifar4429
    @afshinkaramifar44297 ай бұрын

    Such a great explanation, Well done Hamed👏

  • @hamedbahram

    @hamedbahram

    7 ай бұрын

    Thank you! 🫡

  • @jazsouf
    @jazsouf7 ай бұрын

    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

    @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.

  • @tatsumii1420
    @tatsumii14206 ай бұрын

    we would love a framer motion video with nextjs

  • @hamedbahram

    @hamedbahram

    6 ай бұрын

    Absolutely! That has been overdue. Will work on it.

  • @nikhilpsathyanathan
    @nikhilpsathyanathan5 ай бұрын

    I thought it just like layout but this video is awesome. Got to know more about template' functionality

  • @hamedbahram

    @hamedbahram

    5 ай бұрын

    Glad it was helpful!

  • @Felipe-pb9gu
    @Felipe-pb9gu8 ай бұрын

    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

    @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.

  • @maskman4821
    @maskman48218 ай бұрын

    Great tutorial ❤

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    Thanks! Glad you found it helpful.

  • @CHIRANJIBNANDY1
    @CHIRANJIBNANDY12 ай бұрын

    beautifully explained

  • @hamedbahram

    @hamedbahram

    2 ай бұрын

    Thank you! 🙂

  • @user-hc2nc1el5k
    @user-hc2nc1el5k8 ай бұрын

    Best regards from Egypt

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    Welcome to the channel!

  • @raymondmichael4987
    @raymondmichael49878 ай бұрын

    Thanks buddy 😊

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    Any time!

  • @t2e0j0a4
    @t2e0j0a45 ай бұрын

    Hi really loved video. By the way what is the font u are using for VS Code editor

  • @hamedbahram

    @hamedbahram

    5 ай бұрын

    Thanks! I'm using Operator mono.

  • @arihantjain3274
    @arihantjain32747 ай бұрын

    This is Gold

  • @hamedbahram

    @hamedbahram

    7 ай бұрын

    Thanks 🫡

  • @shahryartavakkoli
    @shahryartavakkoli8 ай бұрын

    Thanks

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    Anytime!

  • @toone4068
    @toone40687 ай бұрын

    thank you

  • @hamedbahram

    @hamedbahram

    7 ай бұрын

    You're welcome

  • @mahmoudmohamed-xu2lb
    @mahmoudmohamed-xu2lb8 ай бұрын

    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

    @hamedbahram

    8 ай бұрын

    Yeah that should work, you can use any unique random key or the current path as the key.

  • @2gbeh
    @2gbeh7 ай бұрын

    🇳🇬 Great job

  • @hamedbahram

    @hamedbahram

    7 ай бұрын

    Thanks!

  • @RicardoBarbosaSousa
    @RicardoBarbosaSousa8 ай бұрын

    Could you u use dark theme in the browser as well? 😊

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    I'll try it next time 🙂

  • @LorenzoSemorile
    @LorenzoSemorile4 ай бұрын

    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

    @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

    @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

    @hamedbahram

    4 ай бұрын

    @@LorenzoSemorile Glad to help!

  • @MohammedIbrahim-fl2sx
    @MohammedIbrahim-fl2sx3 ай бұрын

    i have a login page (Auth pages) how can i remove the components in RootLayout in those pages

  • @hamedbahram

    @hamedbahram

    3 ай бұрын

    You can use route groups to create to different layout. Watch this → kzread.info/dash/bejne/eaNo1K5qZcq_hNY.html

  • @samislam2746
    @samislam27466 ай бұрын

    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

    @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.

  • @moazamdev
    @moazamdev6 ай бұрын

    Does the template force the component to re-render?

  • @hamedbahram

    @hamedbahram

    6 ай бұрын

    Yes exactly.

  • @samislam2746
    @samislam27466 ай бұрын

    The mouse pointer in the video is not visible

  • @hamedbahram

    @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.

  • @AllahomAnsorGaza
    @AllahomAnsorGaza4 ай бұрын

    how to pass data we fetched in layout to children pages

  • @hamedbahram

    @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

    @AllahomAnsorGaza

    4 ай бұрын

    you are great teacher ☺️ thanks for all of your efforts

  • @hamedbahram

    @hamedbahram

    4 ай бұрын

    @@AllahomAnsorGaza Thank you! I appreciate that.

  • @tuRistst
    @tuRistst7 ай бұрын

    But if I want to hide the header for about page?

  • @hamedbahram

    @hamedbahram

    7 ай бұрын

    Then don't put it in that route group.

  • @tuRistst

    @tuRistst

    7 ай бұрын

    Thanks, but I found a solution using Route Groups

  • @namesare4fools
    @namesare4fools6 ай бұрын

    your tutorial is excellent but i think content like this can be shorten to at most 10 mins

  • @hamedbahram

    @hamedbahram

    6 ай бұрын

    Thanks for your suggestion.

  • @SanderCokart
    @SanderCokart8 ай бұрын

    I hope fixing exit animations on Templates is high on their roadmap, forced to use pages router for page animations

  • @hamedbahram

    @hamedbahram

    8 ай бұрын

    Hmm 🤔 didn't know there is a problem with exit navigation! I'll give it a try. Thanks for sharing.

  • @SanderCokart

    @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

    @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

    @SanderCokart

    8 ай бұрын

    @@hamedbahram yes only initial and animate. Pages router is required till then.

  • @sharpesthawk
    @sharpesthawk6 ай бұрын

    You are *"Jeff Goldblum"* right? Am I the only one who thinks that?

  • @hamedbahram

    @hamedbahram

    6 ай бұрын

    I'm not that old 😅

  • @deepaksingh-qd7xm
    @deepaksingh-qd7xm2 ай бұрын

    not good explanation

  • @hamedbahram

    @hamedbahram

    2 ай бұрын

    Thanks!

Келесі