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

Поділитися
Вставка
  • Опубліковано 4 лип 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

КОМЕНТАРІ • 217

  • @abidpp51
    @abidpp51 10 місяців тому +1

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

    • @hamedbahram
      @hamedbahram  10 місяців тому +1

      Thanks Abid! Appreciate your comment.

  • @yashpandey350
    @yashpandey350 8 місяців тому +3

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

    • @hamedbahram
      @hamedbahram  8 місяців тому

      Awesome! Glad to hear that...

  • @malsp5151
    @malsp5151 Рік тому +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  Рік тому

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

  • @goku_watches
    @goku_watches 8 місяців тому +1

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

    • @hamedbahram
      @hamedbahram  8 місяців тому

      Glad to hear that! Welcome to the channel 🙂

  • @mathiasriissorensen6994
    @mathiasriissorensen6994 7 місяців тому +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  7 місяців тому +1

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

  • @SGZealotry
    @SGZealotry 5 місяців тому +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  5 місяців тому

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

  • @Petyr25
    @Petyr25 10 місяців тому +1

    This is the video everyone should watch. Absolutely incredible!

    • @hamedbahram
      @hamedbahram  10 місяців тому

      Thanks! I appreciate your comment.

  • @albertoplebani77
    @albertoplebani77 9 місяців тому +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  9 місяців тому

      Great to hear that! I appreciate it.

  • @Shelby63589
    @Shelby63589 6 місяців тому +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  6 місяців тому +1

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

  • @Claudia-hz4ly
    @Claudia-hz4ly 5 місяців тому +1

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

    • @hamedbahram
      @hamedbahram  5 місяців тому

      You're very welcome! Glad it was helpful!

  • @Isaackasongoyo
    @Isaackasongoyo Місяць тому +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  Місяць тому

      Glad to hear you found it helpful.

  • @Shawn-Mosher
    @Shawn-Mosher Рік тому

    Great topic and clearing up any confusion! Well done

  • @TheEngineer-ef1jq
    @TheEngineer-ef1jq Рік тому +1

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

    • @hamedbahram
      @hamedbahram  Рік тому

      Thanks! I appreciate your feedback.

  • @cplus4
    @cplus4 Рік тому +1

    I stumbled across your channel this week and have started binge watching everything. This is by far the best NextJS content on UA-cam. 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  Рік тому +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.

  • @be_kateryna
    @be_kateryna Рік тому +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  Рік тому +3

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

  • @abhilashkumar4753
    @abhilashkumar4753 9 місяців тому +1

    Incredible, clear explanation of concepts 😮

    • @hamedbahram
      @hamedbahram  9 місяців тому

      Thanks! I appreciate it!

  • @chriseski
    @chriseski Рік тому +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  Рік тому +1

      Glad to hear that Chris! I appreciate your comment.

    • @yash_yadav3733
      @yash_yadav3733 7 місяців тому

      i had same issue bro, so true!

  • @filixzs
    @filixzs Рік тому

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

    • @hamedbahram
      @hamedbahram  Рік тому

      Great to hear that Felix! Appreciate your comment.

  • @thehamzajunaid
    @thehamzajunaid 8 місяців тому +1

    very informative Hamed. Thanks for this.

    • @hamedbahram
      @hamedbahram  8 місяців тому +1

      Glad to hear that Hamza!

  • @user-fr8qp8ui9e
    @user-fr8qp8ui9e 11 місяців тому +1

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

  • @li-anchen9768
    @li-anchen9768 2 місяці тому +1

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

    • @hamedbahram
      @hamedbahram  2 місяці тому

      My pleasure! Glad it helped 🙂

  • @imkir4n
    @imkir4n Рік тому +1

    This video gives me so much insight about server components👍

  • @ukaszkunicki293
    @ukaszkunicki293 Рік тому

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

    • @hamedbahram
      @hamedbahram  Рік тому +1

      Glad it was helpful! Appreciate your comment.

  • @sayeedmahdimousavi351
    @sayeedmahdimousavi351 Рік тому

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

    • @hamedbahram
      @hamedbahram  Рік тому

      You are welcome! Glad to hear that.

  • @naveedalirehmani4135
    @naveedalirehmani4135 9 місяців тому +1

    great video exactly what I was looking for!

  • @shared8970
    @shared8970 Рік тому

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

  • @ahmetcanisik
    @ahmetcanisik Місяць тому +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.

  • @heysahilsingh
    @heysahilsingh 7 місяців тому +1

    Really loving your channel ❤

  • @raulparra4678
    @raulparra4678 Рік тому

    Amazing explanation! Thanks.

    • @hamedbahram
      @hamedbahram  Рік тому

      Glad it was helpful! Appreciate you comment.

  • @udaym4204
    @udaym4204 4 місяці тому +1

    Thank you Hamed 😃👍

  • @enafor99
    @enafor99 Рік тому

    greate tutorial Ahmed , thanks so much👋❤

  • @user-pq8lo1eu2j
    @user-pq8lo1eu2j 4 місяці тому +1

    Brilliant video!

  • @igboanugwocollins4452
    @igboanugwocollins4452 11 місяців тому

    Awesome video, learnt alot

  • @saeidalidadi7579
    @saeidalidadi7579 10 місяців тому +1

    Interesting and awesome Next.js video.

  • @patrickc.2680
    @patrickc.2680 9 місяців тому +1

    Extremely helpful video, I just subscribed to this channel.

  • @jean-pierrebanchereau8474
    @jean-pierrebanchereau8474 Рік тому

    Very interesting study case. Thank's a lot

    • @hamedbahram
      @hamedbahram  Рік тому

      My pleasure! appreciate your comment.

  • @AaronTMac
    @AaronTMac 9 місяців тому +1

    Damn this was beautifully explained.

    • @hamedbahram
      @hamedbahram  9 місяців тому +1

      Glad to hear that! Appreciate your comment 😃

  • @rockNbrain
    @rockNbrain 10 місяців тому +1

    great job bro, tks a lot!

  • @un_chien_andalou
    @un_chien_andalou 9 місяців тому +1

    Thanks, man.

  • @hbela1000
    @hbela1000 Рік тому

    top-notch,thx.

  • @raphauy
    @raphauy Рік тому

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

    • @hamedbahram
      @hamedbahram  Рік тому

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

  • @mehdinaji2389
    @mehdinaji2389 11 місяців тому

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

  • @Ivan-Shyriaiev
    @Ivan-Shyriaiev 9 місяців тому +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  9 місяців тому

      Glad to hear that! I appreciate your comment 🙂

  • @monirshimul
    @monirshimul 10 місяців тому +2

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

    • @hamedbahram
      @hamedbahram  10 місяців тому

      Good idea! I'll have that in mind.

  • @soaringeaglet4956
    @soaringeaglet4956 9 місяців тому +1

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

    • @hamedbahram
      @hamedbahram  9 місяців тому

      Thanks, I appreciate it!

  • @muhammahanisuzzaman5493
    @muhammahanisuzzaman5493 Рік тому

    Jajakallah... Bro💯

  • @mahesh-waghmare
    @mahesh-waghmare Рік тому

    Amazing 🎉

  • @user-lo5tl6wn9b
    @user-lo5tl6wn9b 9 місяців тому +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  9 місяців тому

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

  • @usamausman5225
    @usamausman5225 Рік тому +1

    Sir please do a complete series on typescript.

    • @hamedbahram
      @hamedbahram  Рік тому

      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:
      ua-cam.com/video/XmWGqi0pskU/v-deo.html
      ua-cam.com/video/8e35eo447Zw/v-deo.html

  • @JiNx-yf1ef
    @JiNx-yf1ef Рік тому

    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  Рік тому

      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 Рік тому +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  Рік тому +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.

  • @aminsoraya4517
    @aminsoraya4517 8 місяців тому +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  8 місяців тому

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

    • @aminsoraya4517
      @aminsoraya4517 8 місяців тому

      yes you did it@@hamedbahram

  • @lemondigitaldesign
    @lemondigitaldesign 8 місяців тому +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  8 місяців тому

      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 8 місяців тому +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  8 місяців тому

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

  • @amancharlamanas3812
    @amancharlamanas3812 11 місяців тому +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  11 місяців тому

      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.

  • @KamilDrozdz
    @KamilDrozdz 11 місяців тому +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  11 місяців тому

      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.

  • @qusaijabarisada
    @qusaijabarisada 8 місяців тому +4

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

    • @hamedbahram
      @hamedbahram  8 місяців тому

      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.

  • @CasualDrive
    @CasualDrive 4 місяці тому +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  4 місяці тому +1

      You can fetch data the user data on the server.

  • @giovannitonussi3746
    @giovannitonussi3746 Рік тому +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  Рік тому

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

    • @giovannitonussi3746
      @giovannitonussi3746 Рік тому

      @@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  Рік тому +1

      @@giovannitonussi3746 yeah that'd be interesting 🤔

  • @jobiej7416
    @jobiej7416 Рік тому +1

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

    • @hamedbahram
      @hamedbahram  Рік тому +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 11 місяців тому +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  11 місяців тому

      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.

  • @AlexSilva-ye2lp
    @AlexSilva-ye2lp Рік тому +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  Рік тому

      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.

  • @jayantsingh7768
    @jayantsingh7768 3 місяці тому +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  3 місяці тому

      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.

  • @user-om8yd1pk4m
    @user-om8yd1pk4m 11 місяців тому

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

    • @hamedbahram
      @hamedbahram  11 місяців тому

      Thanks for the feedback

  • @m__link6499
    @m__link6499 Місяць тому +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  Місяць тому

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

  • @heysahilsingh
    @heysahilsingh 6 місяців тому +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  6 місяців тому

      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.

  • @user-sm3rx2uw7r
    @user-sm3rx2uw7r Рік тому +1

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

    • @hamedbahram
      @hamedbahram  Рік тому

      For sure! Appreciate your feedback.

  • @shinobi_coder88
    @shinobi_coder88 8 місяців тому +1

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

    • @hamedbahram
      @hamedbahram  8 місяців тому

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

  • @user-ks6pv6ui4i
    @user-ks6pv6ui4i 9 місяців тому +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  9 місяців тому

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

  • @oshoham1975
    @oshoham1975 11 місяців тому +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  11 місяців тому

      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 11 місяців тому +1

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

    • @hamedbahram
      @hamedbahram  11 місяців тому

      @@oshoham1975 sweet!

  • @dexcoder1207
    @dexcoder1207 11 місяців тому +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  11 місяців тому +1

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

    • @dexcoder1207
      @dexcoder1207 11 місяців тому +1

      @@hamedbahram thank you so much for your time

    • @hamedbahram
      @hamedbahram  11 місяців тому

      @@dexcoder1207 anytime!

  • @satindersingh9671
    @satindersingh9671 11 місяців тому +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  11 місяців тому

      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 11 місяців тому +1

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

  • @AbdulRahim-zb9zm
    @AbdulRahim-zb9zm 5 місяців тому

    Greaaaaaaaaaaaaaaat

    • @hamedbahram
      @hamedbahram  5 місяців тому

      Thanks!

    • @AbdulRahim-zb9zm
      @AbdulRahim-zb9zm 5 місяців тому

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

  • @ConnorBurge-dk5uj
    @ConnorBurge-dk5uj 3 місяці тому +1

    What camera do you use?

  • @BruceWayne-kw6xm
    @BruceWayne-kw6xm 5 місяців тому

    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  5 місяців тому +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 5 місяців тому +1

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

    • @hamedbahram
      @hamedbahram  5 місяців тому

      @@BruceWayne-kw6xm you're welcome!

  • @Adrian_Galilea
    @Adrian_Galilea 5 місяців тому +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 5 місяців тому

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

    • @Adrian_Galilea
      @Adrian_Galilea 5 місяців тому

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

    • @hamedbahram
      @hamedbahram  5 місяців тому +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 5 місяців тому +1

      @@hamedbahram That is perfect, thank you!

  • @armaandhanji7151
    @armaandhanji7151 Рік тому

    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  Рік тому +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 Рік тому +1

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

    • @hamedbahram
      @hamedbahram  Рік тому

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

  • @aroo1377
    @aroo1377 11 місяців тому +1

    Does using use client causes SSR to break?

    • @hamedbahram
      @hamedbahram  11 місяців тому

      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.

  • @Rehan-iw7jm
    @Rehan-iw7jm 11 місяців тому

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

    • @hamedbahram
      @hamedbahram  11 місяців тому +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 11 місяців тому

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

    • @hamedbahram
      @hamedbahram  11 місяців тому

      @@Rehan-iw7jm For sure!

    • @Rehan-iw7jm
      @Rehan-iw7jm 11 місяців тому +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  11 місяців тому

      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.

  • @hey.............
    @hey............. 11 місяців тому

    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  11 місяців тому

      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............. 11 місяців тому +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  11 місяців тому

      @@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 11 місяців тому +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  11 місяців тому +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 :)

  • @imkir4n
    @imkir4n Рік тому +1

    😀😀😀

  • @humanities1019
    @humanities1019 2 місяці тому +1

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

  • @bogdanalexandru5071
    @bogdanalexandru5071 8 місяців тому +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  8 місяців тому

      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 8 місяців тому

      @@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 8 місяців тому +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 8 місяців тому

      @@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  8 місяців тому

      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.

  • @joe_j
    @joe_j Рік тому +1

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

    • @hamedbahram
      @hamedbahram  Рік тому

      What is the error you're getting?

    • @joe_j
      @joe_j Рік тому

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

    • @joe_j
      @joe_j Рік тому

      On the blogs directory

    • @joe_j
      @joe_j Рік тому

      @@hamedbahram That is the code. UA-cam is kinda deleting my replies

    • @joe_j
      @joe_j Рік тому

      @@hamedbahram joe-jngigi/next_learn_dev

  • @alexon2010
    @alexon2010 Рік тому

    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  Рік тому

      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 Рік тому +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  Рік тому

      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 Рік тому

      @@hamedbahram thank you