Server vs client components in NextJs 13 - When to use which

Sdílet
Vložit
  • čas přidán 13. 06. 2024
  • In this video, we'll compare server and client components, discuss when to use each, and examine common patterns for interleaving them in NextJs 13.
    👉🏼 The Ultimate NextJs Course
    🔗 www.hamedbahram.io/courses/ne...
    👉🏼 Project source code (Github)
    🔗 github.com/HamedBahram/next-s...
    👉🏼 NextJs documentation
    🔗 nextjs.org/docs
    Chapters
    0:00 Intro to server components
    1:50 Why server components
    4:00 Client components
    5:20 When to use which
    7:30 Moving client components to the leaves
    10:30 Nesting a server component in a client component
    12:20 Passing props to client components
    13:00 Avoid poisoning
    14:10 Using 3rd party packages
    16:10 Sharing data between server components
    18:10 Project
    33:40 Outro

Komentáře • 217

  • @li-anchen9768
    @li-anchen9768 Před měsícem +1

    Thank you for the very clear explanation and concrete example which gives me a better grasp of Next.js!

  • @SGZealotry
    @SGZealotry Před 4 měsíci +2

    I'm only 7 minutes into this and felt the need to leave a message. This is fantastically explained. I've watched many other videos but non have explained it as well as you. Thank you.

    • @hamedbahram
      @hamedbahram  Před 4 měsíci

      Thank you! I appreciate that. Im glad you found it helpful.

  • @markspicer5511
    @markspicer5511 Před rokem +1

    This is an excellent video! Very clear and well explained with perfect examples. Thank you Hamed, another great piece of work from you - really helpful. You have the ability to explain complex things in a simple to understand way. Appreciated by me and many others, I'm sure.

    • @hamedbahram
      @hamedbahram  Před rokem

      Thank you for your kind words Mark. I'm glad you found it helpful. Appreciate your comment.

  • @mathiasriissorensen6994
    @mathiasriissorensen6994 Před 6 měsíci +1

    I've searched for the proper channel for a long time, and I must admit that became in love with your communiation style almost instantly. This is where I learn the best. Thank you for doing this!

    • @hamedbahram
      @hamedbahram  Před 6 měsíci +1

      Thanks for sharing. I'm glad to hear that. Welcome onboard!

  • @Shelby63589
    @Shelby63589 Před 5 měsíci +2

    wow wow wow!!!! you have no idea how much information and key points you provided in just 34 minutes. thanks a lot😇🤩

    • @hamedbahram
      @hamedbahram  Před 5 měsíci +1

      Thank you! I appreciate your comment. Glad you found it helpful.

  • @yashpandey350
    @yashpandey350 Před 7 měsíci +3

    2:10 So finally I understood the real thing behind the madness behind adopting nextjs❣❣

  • @Isaackasongoyo
    @Isaackasongoyo Před měsícem +1

    I just read everything I needed about nextJS new direction in just less than 35 min and it worth it. Great content

    • @hamedbahram
      @hamedbahram  Před měsícem

      Glad to hear you found it helpful.

  • @chriseski
    @chriseski Před rokem +9

    Struggled about how to share state across the whole app with server components, and now you made it so clear and easy through that small example project. That was literally you answering my question exactly as I needed to! Thank you Hamed that was amazing explanation 👌🙏

    • @hamedbahram
      @hamedbahram  Před rokem +1

      Glad to hear that Chris! I appreciate your comment.

    • @yash_yadav3733
      @yash_yadav3733 Před 7 měsíci

      i had same issue bro, so true!

  • @abidpp51
    @abidpp51 Před 9 měsíci +1

    A remarkably clear and exceptional video providing insights into Next.js version 13 , Thank you Hamed !

    • @hamedbahram
      @hamedbahram  Před 9 měsíci +1

      Thanks Abid! Appreciate your comment.

  • @goku_watches
    @goku_watches Před 8 měsíci +1

    Excellent explanation. Just delving into NextJs, wanted a clear explanation, and you've successfully provided one. Thank you

    • @hamedbahram
      @hamedbahram  Před 8 měsíci

      Glad to hear that! Welcome to the channel 🙂

  • @Claudia-hz4ly
    @Claudia-hz4ly Před 4 měsíci +1

    I loved the way this is explained! You made this so simple to understand! Thank you so much!

    • @hamedbahram
      @hamedbahram  Před 4 měsíci

      You're very welcome! Glad it was helpful!

  • @cplus4
    @cplus4 Před 11 měsíci +1

    I stumbled across your channel this week and have started binge watching everything. This is by far the best NextJS content on CZcams. I started using next properly a few months ago and having come from a React/Node(Express) mind set, I found it tricky to make a comfortable transition into NextJS. So hard to leave behind the routes/controllers/middleware folder structures 😭 I haven't found any channel that talks about transitioning your mindset from Node/Express to NextJS (maybe because most devs are still trying to figure it out!) but your way of explaining things are so clean and refreshing to listen to, I have managed to see NextJS from a different viewpoint and am starting to totally get it and feel more confident with it. I feel like my imposter syndrome is now cured! Looking forward to seeing more content in the future 😎 EDIT: oh and I finally understand how to use next/image to display my images as I want them!

    • @hamedbahram
      @hamedbahram  Před 11 měsíci +2

      Thanks for your kind feedback, I appreciate it. I'm very glad I was able to help you transition from MERN to using NextJs full stack.

  • @albertoplebani77
    @albertoplebani77 Před 8 měsíci +1

    This video is awesome, it should included directly into the nextjs app router documentation! Seriously, it was two days i was trying to understand these differences coming from 4 years of page directory structure and your video is extremely clear. I'm gonna see also all the other videos you published!

    • @hamedbahram
      @hamedbahram  Před 8 měsíci

      Great to hear that! I appreciate it.

  • @katerynabiehantseva2744
    @katerynabiehantseva2744 Před rokem +7

    Please, make a video about internazionalization with server and clients components. And cover how to pass in corrrect way translations and intl usage if yoy want create an array of translations. Its inportant topic

    • @hamedbahram
      @hamedbahram  Před rokem +3

      For sure Kateryna. I'll cover it this week. Appreciate your feedback.

  • @TheEngineer-ef1jq
    @TheEngineer-ef1jq Před rokem +1

    Fantastic, please do more of these type of deep dive videos where you cover the theory well. its missing on most channels today

  • @Petyr25
    @Petyr25 Před 9 měsíci +1

    This is the video everyone should watch. Absolutely incredible!

    • @hamedbahram
      @hamedbahram  Před 9 měsíci

      Thanks! I appreciate your comment.

  • @thehamzajunaid
    @thehamzajunaid Před 7 měsíci +1

    very informative Hamed. Thanks for this.

  • @ahmetcanisik
    @ahmetcanisik Před měsícem +1

    I finally understood and the restlessness inside me disappeared. I couldn't help but get angry at myself, comparing me, who was hectic, and you, who was relaxed while coding.

  • @user-fr8qp8ui9e
    @user-fr8qp8ui9e Před 10 měsíci +1

    nice job man, awesome, i love the way you described server and client components by help of your hand and the boundary.
    thanks

  • @patrickc.2680
    @patrickc.2680 Před 9 měsíci +1

    Extremely helpful video, I just subscribed to this channel.

  • @Shawn-Mosher
    @Shawn-Mosher Před rokem

    Great topic and clearing up any confusion! Well done

  • @naveedalirehmani4135
    @naveedalirehmani4135 Před 8 měsíci +1

    great video exactly what I was looking for!

  • @abhilashkumar4753
    @abhilashkumar4753 Před 8 měsíci +1

    Incredible, clear explanation of concepts 😮

  • @ukaszkunicki293
    @ukaszkunicki293 Před rokem

    Great material, easy to understand and contains every important information.

    • @hamedbahram
      @hamedbahram  Před rokem +1

      Glad it was helpful! Appreciate your comment.

  • @filixzs
    @filixzs Před rokem

    Amazing video to understand server components and how to use them together with client components to architecture apps!

    • @hamedbahram
      @hamedbahram  Před rokem

      Great to hear that Felix! Appreciate your comment.

  • @imkir4n
    @imkir4n Před rokem +1

    This video gives me so much insight about server components👍

  • @heysahilsingh
    @heysahilsingh Před 6 měsíci +1

    Really loving your channel ❤

  • @shared8970
    @shared8970 Před 11 měsíci

    Great explanation of what Server Components vs. Client Components is all about, why we should care. Bookmarking and subscribing

  • @enafor99
    @enafor99 Před rokem

    greate tutorial Ahmed , thanks so much👋❤

  • @sayeedmahdimousavi351

    I had all these problems with server and client site components this video helped me alot, thanks dear teacher🌹

  • @saeidalidadi7579
    @saeidalidadi7579 Před 9 měsíci +1

    Interesting and awesome Next.js video.

  • @igboanugwocollins4452
    @igboanugwocollins4452 Před 11 měsíci

    Awesome video, learnt alot

  • @user-pq8lo1eu2j
    @user-pq8lo1eu2j Před 3 měsíci +1

    Brilliant video!

  • @raulparra4678
    @raulparra4678 Před rokem

    Amazing explanation! Thanks.

    • @hamedbahram
      @hamedbahram  Před rokem

      Glad it was helpful! Appreciate you comment.

  • @AaronTMac
    @AaronTMac Před 8 měsíci +1

    Damn this was beautifully explained.

    • @hamedbahram
      @hamedbahram  Před 8 měsíci +1

      Glad to hear that! Appreciate your comment 😃

  • @jean-pierrebanchereau8474

    Very interesting study case. Thank's a lot

  • @udaym4204
    @udaym4204 Před 4 měsíci +1

    Thank you Hamed 😃👍

  • @soaringeaglet4956
    @soaringeaglet4956 Před 8 měsíci +1

    Great resource, I hope you blow up! weekend binge watch material.

  • @rockNbrain
    @rockNbrain Před 9 měsíci +1

    great job bro, tks a lot!

  • @raphauy
    @raphauy Před rokem

    One of the best videos I've seen recently. if you used typescript it would be perfect 😁 Thanks for sharing!

    • @hamedbahram
      @hamedbahram  Před rokem

      Appreciate your comment Raphael! I'll do more TypeScript in future videos.

  • @Ivan-Shyriaiev
    @Ivan-Shyriaiev Před 8 měsíci +1

    Thank u so much !
    Next.js documentation says next:
    "use client" is used to declare a boundary between a Server and Client Component modules. This means that by defining a "use client" in a file, all other modules imported into it, including child components, are considered part of the client bundle - and will be rendered by React on the client.
    I Read this and thought that anything passed to a client component became client component.
    Even though it says: all other modules IMPORTED into it, including child components.....
    Yeah, my bad :(
    I re-search the whole internet about 'how to create a theme provider in next.js so that the app remains rendering on the server side' and found nothing.
    And you explained this perfectly.
    Thank u, i appreciate this:)

    • @hamedbahram
      @hamedbahram  Před 8 měsíci

      Glad to hear that! I appreciate your comment 🙂

  • @user-lo5tl6wn9b
    @user-lo5tl6wn9b Před 8 měsíci +1

    Brilliant!!! Thank you for this great explanation for such a confusing matter for me! Btw the whole video I'm imagining you're a scientist explaining things in a lab because of that white shirt lol! I suggest you embrace it, it helped me concentrate and really take in what you're explaining, seriously! 🤣🙏

    • @hamedbahram
      @hamedbahram  Před 8 měsíci

      Glad to hear you found it helpful! I'll definitely use the white shirt more 😂

  • @un_chien_andalou
    @un_chien_andalou Před 9 měsíci +1

    Thanks, man.

  • @mehdinaji2389
    @mehdinaji2389 Před 10 měsíci

    اقا حامد کار درست ❤❤

  • @hbela1000
    @hbela1000 Před rokem

    top-notch,thx.

  • @monirshimul
    @monirshimul Před 9 měsíci +2

    Please do a little larger project, about this whole concept with the combination of client and server side rendering.

    • @hamedbahram
      @hamedbahram  Před 9 měsíci

      Good idea! I'll have that in mind.

  • @muhammahanisuzzaman5493
    @muhammahanisuzzaman5493 Před 11 měsíci

    Jajakallah... Bro💯

  • @mahesh-waghmare
    @mahesh-waghmare Před rokem

    Amazing 🎉

  • @amancharlamanas3812
    @amancharlamanas3812 Před 10 měsíci +1

    This video is really helpful, thanks a lot!! Also a small doubt, the preload warnings that you get in 32:56 , how to get rid of those?

    • @hamedbahram
      @hamedbahram  Před 10 měsíci

      Glad it was helpful. The warnings are for pre-loading css in dev, you don't need to worry about it. To disable prefetching in your `link` components you can pass the `prefetch={false}` prop.

  • @usamausman5225
    @usamausman5225 Před rokem +1

    Sir please do a complete series on typescript.

    • @hamedbahram
      @hamedbahram  Před rokem

      I appreciate your feedback Usama. In this particular video Typescript wouldn't have made much of a difference. I have a couple of videos using Typescript in NextJs 13, also with prisma:
      czcams.com/video/XmWGqi0pskU/video.html
      czcams.com/video/8e35eo447Zw/video.html

  • @CasualDrive
    @CasualDrive Před 3 měsíci +1

    Great explanation thank you! What if the page is about current user information like a profile page. isn't every bit of information on the page listed is somehow becomes client component just because we need to get user name from URL and fetch data from API to show related info on the page?

    • @hamedbahram
      @hamedbahram  Před 3 měsíci +1

      You can fetch data the user data on the server.

  • @KamilDrozdz
    @KamilDrozdz Před 11 měsíci +1

    thank you very much for the information, but I'm curious about a few things if we have fetching on the client and server side, how to secure the paths there is little information on this subject and the second thing is whether using the backend in Java makes any difference in this puzzle?

    • @hamedbahram
      @hamedbahram  Před 11 měsíci

      I have a couple of videos where I show how to protect your server vs client components/pages in NextJs 13 using the `next-auth` package as well as Clerk.

  • @giovannitonussi3746
    @giovannitonussi3746 Před 11 měsíci +1

    Another great video, thanks a lot for sharing so much information! One question: How then to make an UI lib that is compatible with server components?

    • @hamedbahram
      @hamedbahram  Před 11 měsíci

      Thanks Geovanni! For UI libraries you just need to wrap them in client components using the "use client" directive.

    • @giovannitonussi3746
      @giovannitonussi3746 Před 11 měsíci

      @@hamedbahram Thanks for the reply man! I did it and it really works, but what if I want to build this UI in a monorepo with server components to share between several Next projects? I would love to see your opinion on this on a vid maybe!

    • @hamedbahram
      @hamedbahram  Před 11 měsíci +1

      @@giovannitonussi3746 yeah that'd be interesting 🤔

  • @JiNx-yf1ef
    @JiNx-yf1ef Před rokem

    Wow, what an incredible video! Thank you so much ❤️ I have a question: Do you think it's beneficial to utilize React Query for managing server-side state? cuz we can now fetch data using RSC and mutate data with server actions, I'm curious about your thoughts on incorporating React Query or even SWR with Next.js. Thank you once again for this amazing content! ❤️

    • @hamedbahram
      @hamedbahram  Před rokem

      Thanks for your kind words. SWR or React query are suitable for fetching data on the client. However it's recommended to move your data fetching to your server components server side. You can utilize the NextJs cache to store the result of your data fetching for subsequent requests.

  • @naderelmahdy
    @naderelmahdy Před rokem +1

    Amazing, can you make a video on using clerk webhooks to sync your data to a database, there are pretty much no articles or videos on clerk webhooks

    • @hamedbahram
      @hamedbahram  Před rokem +1

      That's a good idea, thanks for the suggestion. I have an update video coming up on Clerk, I'll cover webhooks in it as well.

  • @jobiej7416
    @jobiej7416 Před 11 měsíci +1

    Thanks. How would you handle a “load more” scenario where the initial and subsequent data is rendered by a server component

    • @hamedbahram
      @hamedbahram  Před 11 měsíci +1

      Good question! there are different ways to go about this, you can use the `searchParams` in the URL and read it in your server component to fetch data. I'll have a video on this topic coming out next week, so stay tuned.

  • @LuisCastroem
    @LuisCastroem Před 10 měsíci +1

    Hey! Thank you for this video. I stumbled upon it because I’m trying to understand how to update data that I rendered using a server component (aka the data was sent to the elements of the page on the server) and that I want to push a new element to that data when the user adds it via a form without doing a full page refresh but I still don’t seem to find/understand how to

    • @hamedbahram
      @hamedbahram  Před 10 měsíci

      If you're using server actions on your form to update the data, you can call the `revalidatePath` function on the server to refresh the data. If you're hitting an API from your form, you can then use the `useTransition` hook and call `router.refresh` inside of it to refresh the page without a full page reload.

  • @lemondigitaldesign
    @lemondigitaldesign Před 7 měsíci +1

    Hi, thanks for this very clear explanation, it confirms in an easy to understand way what I took from reading the official documentation. Good job! :)
    I do have some remaining doubts on overall architecture of my UI. Taking your blog page example and adding my restraints, maybe you could share your thoughts. What if:
    - my sidebar is collapsible. So it has user action, it needs to show or hide depending on user choice. So I need to wrap it in a client side component that answers to a state
    - my blog posts sit inside tabs or accordion. I need to wrap my blog content in a client side component because again, content shows or hides depending on user interaction and state
    - my logo depends on device/window size. I need to access the window object to decide whether I want a mobile or desktop version of the logo.
    - my footer has a desktop or mobile layout, I need to decide which one to load. Again, I need to access the window object or a 3rd party library that uses hooks
    - if I added a table to display data I may want to make it sortable or filterable - again content, or presentation of content, will be subject to user interaction
    - what if I wanted to add a display toggler from say grid to list for my blog posts. I would need to wrap them in a client side layout component that answers to user input and state
    In the end, everything on my page is subject to some sort of user interaction. I cannot imagine a UI I have built over the last few years that simply loads static content in an isolated manner.
    How would you address the above? Import server side components for content as children into a client component for presentation for every instance? Would it even make sense to use server components at all for this?
    Just have the layout on the server side and fill it up with client side components? Then, again, I hardly need the server side at all!
    For the largest part, when I look at complex UIs I struggle to find instances where I load content which does not need any user interaction or any browser interaction at all.
    The only useful instance I can imagine is to load placeholder data instantly from server side for SEO purposes and then replace it with the client side component once it's ready.
    Looking forward to hearing your thoughts :)

    • @hamedbahram
      @hamedbahram  Před 7 měsíci

      Some of the functionalities you mentioned, like different logos and footers, can be done using CSS media queries instead of JS. That said, user interactivity is an inevitable part of any application, and there is nothing wrong with using client components. Server components are not meant to replace client components. You can benefit from your server infrastructure by keeping dependencies on the server rather than shipping more JS to the client, resulting in a faster page load and, hence, a better user experience. You can then sprinkle client components where user interactivity is needed.
      The App router, built on top of Server components and suspense, also has a better DX. You can fetch data inside your component instead of having to use `getStaticProps` or similar APIs and stream the response to the client. Layouts, suspense, and error boundaries are native and just a file in each segment.
      The new architecture may only be necessary or proper for some applications. You can choose what works best for your specific application needs.

    • @lemondigitaldesign
      @lemondigitaldesign Před 7 měsíci +1

      @@hamedbahram thanks for your reply :)
      Yes, I was a bit dramatic, just to make a point. :)
      I am trying to convert an existing UI to best make use of the new tech and find myself at a point where I realise: basically everything is interactive, even a simple thing like a page footer.
      Data fetching inside components definitely is the most useful feature for my needs, together with streaming UI components which I neeed to look more into.
      Media queries can go a long way (although you may then load redundant files into your HTML - there is never a perfect solution :))
      Thanks for taking the time to post your clarifying thoughts! 😀

    • @hamedbahram
      @hamedbahram  Před 7 měsíci

      @@lemondigitaldesign My pleasure! You're right there won't be a perfect solution, nor a one-size-fits-all.

  • @oshoham1975
    @oshoham1975 Před 10 měsíci +1

    Hi, first of all - Great video! You explained beautifully the distinction between server & client components. I'm new to next.js (since yesterday 🙂) and it really sheds some light.
    I have a question that I hope you can assist with:
    I have a React CSR app that I'd like to migrate to next.js, mainly for SEO reasons. I'm using the MUI component library, including its theming API, which is executed in the client. I can't figure out a way to use it in my server components... Do you know a trick?
    Thanks, Oren

    • @hamedbahram
      @hamedbahram  Před 10 měsíci

      Hi Oren! Thanks for your comment, I appreciate it. Unfortunately MUI doesn't support the App router in NextJs 13 yet, it might come in future. But for now you need to wrap third-party component that are meant to run on the client in your own client component using the `use client` directive.

    • @oshoham1975
      @oshoham1975 Před 10 měsíci +1

      @@hamedbahram Thanks, I figured it out, I applied a similar pattern to the 'providers' pattern that you demonstrated.

    • @hamedbahram
      @hamedbahram  Před 10 měsíci

      @@oshoham1975 sweet!

  • @user-ks6pv6ui4i
    @user-ks6pv6ui4i Před 8 měsíci +1

    This is an excellent video👌🙏. I do have 1 scenario which is partially solved by the example you shared.
    I have a page which is using 'use client' tag. And there is child component which is also client side rendered. From the parent(page) I was passing few props to the child, but now I also have to pass a context which is server side component to the child. In this scenario I am either able to pass children(context) or props from parent. Is there a way to pass both?

    • @hamedbahram
      @hamedbahram  Před 8 měsíci

      I'm not sure if I understand the question, but if you're wondering if you can pass props as well as `children`, the answer is yes you can. `children` is also a prop itself, so you can pass other props together with `children`. If this is not your question, please expand on it...

  • @jayantsingh7768
    @jayantsingh7768 Před 2 měsíci +1

    Hi, first of all, great video. I have a question regarding client-side components. In the early stage of the video, you mentioned that every component inside the client component becomes the client component. However, later on, for instance, with the ThemeProvider, we're creating a child component and using the children there to use the server component inside the client component. So, how are these terms different from one another? You mentioned in the video that if we import the server component inside the client component, then it becomes the client component, but if we use it like "children," then it will not. Can you explain the reason behind this? Thanks.

    • @hamedbahram
      @hamedbahram  Před 2 měsíci

      The difference is when you import vs pass a server component as props (children). When you import, you pass the server-client boundary marked by `use client` directive and therefor the server component is treated as a client component as far as your bundler is concerned. When passed as props this is not the case.

  • @qusaijabarisada
    @qusaijabarisada Před 7 měsíci +4

    Anyone else thinking this comes with a considerable development overhead compared to previous versions? FE development is becoming increasingly complicated

    • @hamedbahram
      @hamedbahram  Před 7 měsíci

      It's definitely different from the patterns we've been used to, but once you get over the hump, it actually simplifies the development in my opinion. For example, data fetching doesn't require any extra NextJs specific function like the `getStaticProps` and `getServerSideProps` instead you fetch data right inside your server components. Layouts, loading UI, error boundaries are native in the app router and doesn't require any extra work from the developer, and many more... Once you start building with the new App router you won't go back.

  • @aminsoraya4517
    @aminsoraya4517 Před 7 měsíci +1

    Although a newer version of next.js have high performance for operations and best facilities. But it have some limitations. For example I can not callback up from client component to server component (Scenario for using intersection observer to trigger a function when an specific img is in view . I mean set src when img in viewport)

    • @hamedbahram
      @hamedbahram  Před 7 měsíci

      Yes you can using server actions. Look at the video I did on Infinite scrolling.

    • @aminsoraya4517
      @aminsoraya4517 Před 7 měsíci

      yes you did it@@hamedbahram

  • @AlexSilva-ye2lp
    @AlexSilva-ye2lp Před rokem +1

    I have a question. I was using framer-motion with this new nextjs version and when ever I use the framer-motion library in a page I have to make the page a client side component. so if I have a site with animations in every page, all of my pages will have to be a client side component. Is this a bad thing ?

    • @hamedbahram
      @hamedbahram  Před rokem

      It's not bad, but you won't benefit from React server components. Instead of turning the whole page into a client component, try abstracting the animating components into isolated client components and keep your pages as server components.

  • @m__link6499
    @m__link6499 Před 22 dny +1

    Hi Hamed, I follow almost the same step, the only difference is instead of importing themeprovider in the provider component like you do, I have imported use stete and use effect. My goal is simply to run a normal JSX element between the opening and closing bracket of the child component in the parent component. So, when I applied the child component in a parent component, I got an error message saying : 'Objects are not valid as a react child(found object with keys children). If you meant to render a collection of children, you an array instead.' Also, if the opening and closing brackets of the child component is empty, I have an error message saying : 'property 'children' is missing in type {''} but required in type '{children:any}'. What did I make wrong? thank you!

    • @hamedbahram
      @hamedbahram  Před 22 dny

      I'm not sure if I understand your question?!

  • @Adrian_Galilea
    @Adrian_Galilea Před 4 měsíci +1

    Hello Hamed,
    There's some remarks that you speak about, not being able to put a server component inside a client component(without turning it into a client component), around 4:00, but in your most recent videos you mention that indeed you can do that.
    Is this something that recently changed?

    • @Adrian_Galilea
      @Adrian_Galilea Před 4 měsíci

      Referring to this video, `How to Add Page Transitions in NextJs 14` also around 4:00

    • @Adrian_Galilea
      @Adrian_Galilea Před 4 měsíci

      Wait, I think you clarify it later on, still a bit confused.

    • @hamedbahram
      @hamedbahram  Před 4 měsíci +1

      You can pass a server component to a client component as props or as a children but you cannot import a server component into a client component. Hope this clarifies the confusion.

    • @Adrian_Galilea
      @Adrian_Galilea Před 4 měsíci +1

      @@hamedbahram That is perfect, thank you!

  • @heysahilsingh
    @heysahilsingh Před 5 měsíci +1

    I have a scenario where I need to use a server component inside a loop of client components. This means a map method is running and returning a server component. How can I achieve this?
    Also, if I pass a server component as a prop to a client and that server component requires some props to be passed in, and those props are in the client component, how can I accomplish this?

    • @hamedbahram
      @hamedbahram  Před 5 měsíci

      What is the scenario that requires this? You can use server components the same way you'd use any component in a map loop; as far as passing props you can use the URL to keep and pass the state from client to server.

  • @dexcoder1207
    @dexcoder1207 Před 10 měsíci +1

    I have a application where i'm creating static pages for the dynamic routes and there is an add-to-cart button. So the button should be considered a child in server-side components or do I have to accept the server components into the client components as a children? thank you so much

    • @hamedbahram
      @hamedbahram  Před 10 měsíci +1

      The button can be a client component that's rendered inside your page server components.

    • @dexcoder1207
      @dexcoder1207 Před 10 měsíci +1

      @@hamedbahram thank you so much for your time

    • @hamedbahram
      @hamedbahram  Před 10 měsíci

      @@dexcoder1207 anytime!

  • @shinobi_coder88
    @shinobi_coder88 Před 8 měsíci +1

    11:13 Hi Hamed, what if we wanna pass props from Client to Server component? how can we do that?

    • @hamedbahram
      @hamedbahram  Před 8 měsíci

      You can use the URL as a way to pass props to the server.

  • @user-om8yd1pk4m
    @user-om8yd1pk4m Před 10 měsíci

    nice..
    great teaching
    ...only synchronize your voice Sir

  • @satindersingh9671
    @satindersingh9671 Před 11 měsíci +1

    I have a server ui component that conditionally render in Navbar according to the user if its present in cookies or not. It will either display logout button or signin button. My problem is, its not updating ui by clicking signin or logout button ui using server actions even after revalidating paths. I need to manually referesh the page for nabar ui to change. Do you know how can I go about that. Thank you

    • @hamedbahram
      @hamedbahram  Před 11 měsíci

      I'm not sure why that is, I'd have to look at your code to understand what's happening but try exporting the `dynamic` route config and set it to `force-dynamic` and see if that makes a difference.

    • @satindersingh9671
      @satindersingh9671 Před 11 měsíci +1

      I'll try make a little example project and share it with you. Thank you

  • @AbdulRahim-zb9zm
    @AbdulRahim-zb9zm Před 4 měsíci

    Greaaaaaaaaaaaaaaat

    • @hamedbahram
      @hamedbahram  Před 4 měsíci

      Thanks!

    • @AbdulRahim-zb9zm
      @AbdulRahim-zb9zm Před 4 měsíci

      @@hamedbahram brother I have a doubt. Kindly give some tips if u ever get some free time. my company uses nextjs for the frontend and python django for backend. Right now for authentication we use jwt tokens .so i use the react use effect and redux toolkit to check the authentication. Is there any other methods u could suggest in a nextjs way

  • @armaandhanji7151
    @armaandhanji7151 Před rokem

    Just a quick question...around 9:05 you mention that when React renders a server component and encounters a client component, it leaves a little slot for it...and once the content reaches the client side, the client components will be rendered and the slots will be filled. But...if you client components are pre-rendered on the server and merely hydrated in the browser...doesn't that mean there shouldn't be any "slots" or "holes" regarding the content sent to the browser from the server? Shouldn't all the html already be generated for them?

    • @hamedbahram
      @hamedbahram  Před rokem +1

      There is a difference between React and NextJs in how they render client components. From a React perspective, client components are primarily rendered on the client, so when they are encountered during server renders there would be slots left for them. NextJs on the other hand pre-render client components on the server, so the initial html going to the browser has the client component parts as well.

    • @armaandhanji7151
      @armaandhanji7151 Před rokem +1

      @@hamedbahram You're the best. Thanks Hamed. Hope you continue posting more videos about using RSC...love the great content.

    • @hamedbahram
      @hamedbahram  Před rokem

      @@armaandhanji7151 Anytime! Glad you find the videos useful.

  • @ConnorBurge-dk5uj
    @ConnorBurge-dk5uj Před 2 měsíci +1

    What camera do you use?

  • @bogdanalexandru5071
    @bogdanalexandru5071 Před 8 měsíci +1

    32:00 why is the pre-rendering behavior different when adding the mounted state? Without it, it seems that the component gets pre-rendered on the server using the MoonIcon and there is no second chance on the Client side to re-render this component when resolvedTheme changes, in order to show the SunIcon. Why is it any different after adding the mounted state, i.e., why doesn't the same behavior apply such that "null" is always rendered with no second chance to re-evaluate the output of this component?

    • @hamedbahram
      @hamedbahram  Před 8 měsíci

      I'm not sure if I understand the question correctly. However from a high level, the `resolvedTheme` is not defined on the server since we don't have access to the browser local storage or the user system preferences, therefor we prevent rendering the theme toggle button until mounted on the client using the mounted state. If we render the theme toggle on the server, it may show the wrong theme selected.

    • @bogdanalexandru5071
      @bogdanalexandru5071 Před 8 měsíci

      @@hamedbahram but even if it is initially mounted incorrectly, won't it render properly after the theme gets toggled by the button click?

    • @bogdanalexandru5071
      @bogdanalexandru5071 Před 8 měsíci +1

      @@hamedbahram Ah... I think I understand my confusion now. So the button *would* change to the correct button after the first click, but you are adding the mounted state only to fix the initial button. Thanks!

    • @bogdanalexandru5071
      @bogdanalexandru5071 Před 8 měsíci

      @@hamedbahram So then my question becomes (assuming we have not yet added the mounted state): when the pre-rended button gets to the Client and the resolvedTheme value changes from undefined (it was undefined on the server) to 'dark', doesn't the entire component re-render?

    • @hamedbahram
      @hamedbahram  Před 8 měsíci

      On the client the value doesn't change from `undefined` to `dark`, it will be initialized to `dark` reading from the user browser local storage or system preferences. `next-themes` runs a script to set the correct attributes on the `html` tag before the rest of you application loads. The mismatch only happens when you're rendering UI that depends on the current theme on the server, and to avoid that we're using the `mounted` state to only render theme dependent UI on the client.

  • @Rehan-iw7jm
    @Rehan-iw7jm Před 11 měsíci

    Hi Hamed, I already purchased your next.js course. can you please show the e-commerce project section video? Thank you.

    • @hamedbahram
      @hamedbahram  Před 11 měsíci +1

      Thank you for purchasing the course, Rehan! I appreciate it. I'm finishing the final edits on the ecommerce project mainly merging Clerk authentication with Swell. The lesson should be up in the following days this week.

    • @Rehan-iw7jm
      @Rehan-iw7jm Před 11 měsíci

      @@hamedbahram ok thank you for the update. Can you please inform me in email when the video is already up? Thank you.

    • @hamedbahram
      @hamedbahram  Před 11 měsíci

      @@Rehan-iw7jm For sure!

    • @Rehan-iw7jm
      @Rehan-iw7jm Před 11 měsíci +1

      @@hamedbahram thank you. Do you use redux? I usually use redux for state management, and redux toolkit query for fetching and caching. Do you think we still need redux for next 13? Thank you.

    • @hamedbahram
      @hamedbahram  Před 11 měsíci

      Well, with React server components you'll fetch data on the server, and use HTTP cache. You can still fetch data client-side using libraries like SWR or ReactQuery that have caching built-in. I'm using React context for global state in the projects. If I were to use a state management solution, I would go for something like Zustand. You can still use Redux but I don't see any particular benefit.

  • @BruceWayne-kw6xm
    @BruceWayne-kw6xm Před 5 měsíci

    you said we can not import client components in server components , but then you imported ThemeButton component inside header compoenent , which is server compoenet by default.. can you please clear this ... apart from that salute to the way you explain things.. have discover your channel recently and i must say its one of the top channels for learning next js...

    • @hamedbahram
      @hamedbahram  Před 5 měsíci +1

      You can import client components in server components, but you cannot do the other way around. Meaning you cannot import server components in client components.

    • @BruceWayne-kw6xm
      @BruceWayne-kw6xm Před 5 měsíci +1

      @@hamedbahram thanks for the clarification.. will you make more videos on next js please

    • @hamedbahram
      @hamedbahram  Před 5 měsíci

      @@BruceWayne-kw6xm you're welcome!

  • @user-sm3rx2uw7r
    @user-sm3rx2uw7r Před rokem +1

    Please show us how to send emails forget password and more topics regarding next js

  • @hey.............
    @hey............. Před 11 měsíci

    Currently, I have my data fetching logic in redux as i am using createAsyncThunk and i am using useSelector to select a state from my redux store. I have usecase wherein i get a json from backend and by going through the json, I construct my filter section, now any time user updates a filter, i dispatch an action to re-fetch data for table component.
    I am not able to figure out how to do the same in next.js 13 using server and client components?

    • @hamedbahram
      @hamedbahram  Před 11 měsíci

      There are different ways to do this. You can use URL search parameters to hold your filter state, or you can use context to have your filter state client side and fetch an API endpoint when the filter changes.

    • @hey.............
      @hey............. Před 11 měsíci +1

      ​@@hamedbahram thanks for the reply☺. I am re-posting my question with more detail.
      Currently, I am using redux-toolkit and create-react-app to implement a page wherein I have a filter section and a table. filter section is created by reading a json that i receive by hitting '/filters' endpoint. Anytime user change a filter, I dispatch an action to update the filter slice. In my table component, I am accessing the filter slice using selector and as filter slice change, i re-fetch data for table.
      Coming to the next world.
      the thing is even if i abstract out the filter logic in a separate file and make it run on server, i want to put in the fetched data in redux store. Now if i will access redux store in the same place where i am fetching data, i will have to make it client side component.
      Sorry Hamed for shooting a lot of questions at you. Hope to hear back from you.

    • @hamedbahram
      @hamedbahram  Před 10 měsíci

      @@hey............. I think the flow you explain should work, and that yes you would have to turn the component that uses Redux into a client component. I have a video coming out soon where I'd show a similar concept of implenenting search filters.

    • @arjuns.3752
      @arjuns.3752 Před 10 měsíci +1

      ​@@hamedbahramI have a page with many products, I'm fetching products from a database in sever component and sending it as a prop to client component to render on screen. I want to add filters to it. Should I fetch them in client side as I'm not able to use important client side features

    • @hamedbahram
      @hamedbahram  Před 10 měsíci +1

      @@arjuns.3752 There are different ways to implement this, one way is to use a client component as you mentioned. I'll make a video on this topic soon since it has been brought up a few time :)

  • @aroo1377
    @aroo1377 Před 10 měsíci +1

    Does using use client causes SSR to break?

    • @hamedbahram
      @hamedbahram  Před 10 měsíci

      Not at all. Client components would also pre-render on the server during SSR, you can also pass server components as props or children to client components while remaining server components.

  • @imkir4n
    @imkir4n Před rokem +1

    😀😀😀

  • @joe_j
    @joe_j Před 11 měsíci +1

    Can you explain the layout component in nextjs and how it works,i am getting an error on it

    • @hamedbahram
      @hamedbahram  Před 11 měsíci

      What is the error you're getting?

    • @joe_j
      @joe_j Před 11 měsíci

      @@hamedbahram I also want to understand how it works and do I need it for any new page I make

    • @joe_j
      @joe_j Před 11 měsíci

      On the blogs directory

    • @joe_j
      @joe_j Před 11 měsíci

      @@hamedbahram That is the code. CZcams is kinda deleting my replies

    • @joe_j
      @joe_j Před 11 měsíci

      @@hamedbahram joe-jngigi/next_learn_dev

  • @humanities1019
    @humanities1019 Před 2 měsíci +1

    im comfused why the the styles changed even though you didn't put something like this dark:text-white

  • @alexon2010
    @alexon2010 Před 11 měsíci

    it is very confusing and conflicting interests of client component and server components, while saying: "preferably to server components" unless I need onClic interactivity etc... all the time you have interactivity in component... yeah this relationship of interest is very confusing... I already use react for a long time and I still find it very confusing SSR and the next one seems to have come to make it even more confusing....

    • @hamedbahram
      @hamedbahram  Před 11 měsíci

      Coming from a traditional React application which is rendered primarily on the client, the new features in React 18 like server components can be confusing. It's definitely a new way of building React applications. I hope I can introduce some of the new concepts in these tutorials. Let me know if I can help to clarify some of the stuff you find confusing.

  • @Grishopping
    @Grishopping Před rokem +2

    Thanks for your videos, I had a doubt with this video and the previous one... in relation to what you say in the minute 13:00 avoid poisoning using (import "server only")
    then when I query or add data using prisma I should use this instruction
    import "server only" // ??????????
    import prisma from './prisma'.
    export async function getProducts() {
    try {
    const products = await prisma.product.findMany()
    console.log("prismaTODODO ", products)
    return { products }
    } catch (error) {
    return { error }
    }
    }
    Greetings Jose Grillo from Venezuela

    • @hamedbahram
      @hamedbahram  Před rokem

      The `PrismaClient` won't run on the client and it will fail, plus that your `DATABASE_URL` should also be stored as a private env variable, which means the client code doesn't have access to it. So you won't need the `server-only` package in that case. But as a general good practice you can use the `server-only` package anytime you're creating function that are only ever meant to run on the server.

    • @Grishopping
      @Grishopping Před rokem

      @@hamedbahram thank you