Тёмный

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

Hamed Bahram
Подписаться 162 тыс.
Просмотров 35 тыс.
50% 1

Опубликовано:

 

25 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 217   
@SGZealotry
@SGZealotry 8 месяцев назад
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 8 месяцев назад
Thank you! I appreciate that. Im glad you found it helpful.
@mathiasriissorensen6994
@mathiasriissorensen6994 10 месяцев назад
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 10 месяцев назад
Thanks for sharing. I'm glad to hear that. Welcome onboard!
@chriseski
@chriseski Год назад
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 Год назад
Glad to hear that Chris! I appreciate your comment.
@sipup_and_wander
@sipup_and_wander 10 месяцев назад
i had same issue bro, so true!
@yashpandey350
@yashpandey350 10 месяцев назад
2:10 So finally I understood the real thing behind the madness behind adopting nextjs❣❣
@hamedbahram
@hamedbahram 10 месяцев назад
Awesome! Glad to hear that...
@be_kateryna
@be_kateryna Год назад
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 Год назад
For sure Kateryna. I'll cover it this week. Appreciate your feedback.
@Shelby63589
@Shelby63589 8 месяцев назад
wow wow wow!!!! you have no idea how much information and key points you provided in just 34 minutes. thanks a lot😇🤩
@hamedbahram
@hamedbahram 8 месяцев назад
Thank you! I appreciate your comment. Glad you found it helpful.
@Isaackasongoyo
@Isaackasongoyo 4 месяца назад
I just read everything I needed about nextJS new direction in just less than 35 min and it worth it. Great content
@hamedbahram
@hamedbahram 4 месяца назад
Glad to hear you found it helpful.
@goku_watches
@goku_watches 11 месяцев назад
Excellent explanation. Just delving into NextJs, wanted a clear explanation, and you've successfully provided one. Thank you
@hamedbahram
@hamedbahram 11 месяцев назад
Glad to hear that! Welcome to the channel 🙂
@albertoplebani77
@albertoplebani77 11 месяцев назад
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 11 месяцев назад
Great to hear that! I appreciate it.
@Ivan-Shyriaiev
@Ivan-Shyriaiev Год назад
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 Год назад
Glad to hear that! I appreciate your comment 🙂
@Claudia-hz4ly
@Claudia-hz4ly 8 месяцев назад
I loved the way this is explained! You made this so simple to understand! Thank you so much!
@hamedbahram
@hamedbahram 8 месяцев назад
You're very welcome! Glad it was helpful!
@li-anchen9768
@li-anchen9768 5 месяцев назад
Thank you for the very clear explanation and concrete example which gives me a better grasp of Next.js!
@hamedbahram
@hamedbahram 5 месяцев назад
My pleasure! Glad it helped 🙂
@abidpp51
@abidpp51 Год назад
A remarkably clear and exceptional video providing insights into Next.js version 13 , Thank you Hamed !
@hamedbahram
@hamedbahram Год назад
Thanks Abid! Appreciate your comment.
@TheEngineer-ef1jq
@TheEngineer-ef1jq Год назад
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.
@monirshimul
@monirshimul Год назад
Please do a little larger project, about this whole concept with the combination of client and server side rendering.
@hamedbahram
@hamedbahram Год назад
Good idea! I'll have that in mind.
@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.
@ahmetcanisik
@ahmetcanisik 4 месяца назад
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.
@hamedbahram
@hamedbahram 4 месяца назад
Glad it helped!
@Abolfazl-r9e4h
@Abolfazl-r9e4h Год назад
nice job man, awesome, i love the way you described server and client components by help of your hand and the boundary. thanks
@hamedbahram
@hamedbahram Год назад
Glad it was helpful!
@Petyr25
@Petyr25 Год назад
This is the video everyone should watch. Absolutely incredible!
@hamedbahram
@hamedbahram Год назад
Thanks! I appreciate your comment.
@KamilDrozdz
@KamilDrozdz Год назад
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 Год назад
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.
@cplus4
@cplus4 Год назад
I stumbled across your channel this week and have started binge watching everything. This is by far the best NextJS content on RU-vid. 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 Год назад
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.
@patrickc.2680
@patrickc.2680 Год назад
Extremely helpful video, I just subscribed to this channel.
@hamedbahram
@hamedbahram Год назад
Welcome aboard!
@naveedalirehmani4135
@naveedalirehmani4135 11 месяцев назад
great video exactly what I was looking for!
@hamedbahram
@hamedbahram 11 месяцев назад
Great to hear!
@AyaBochman
@AyaBochman Год назад
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 Год назад
Glad to hear you found it helpful! I'll definitely use the white shirt more 😂
@malsp5151
@malsp5151 Год назад
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.
@imkir4n
@imkir4n Год назад
This video gives me so much insight about server components👍
@hamedbahram
@hamedbahram Год назад
Appreciate your comment!
@soaringeaglet4956
@soaringeaglet4956 Год назад
Great resource, I hope you blow up! weekend binge watch material.
@hamedbahram
@hamedbahram Год назад
Thanks, I appreciate it!
@abhilashkumar4753
@abhilashkumar4753 11 месяцев назад
Incredible, clear explanation of concepts 😮
@hamedbahram
@hamedbahram 11 месяцев назад
Thanks! I appreciate it!
@shared8970
@shared8970 Год назад
Great explanation of what Server Components vs. Client Components is all about, why we should care. Bookmarking and subscribing
@hamedbahram
@hamedbahram Год назад
Glad you found it helpful!
@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 11 месяцев назад
very informative Hamed. Thanks for this.
@hamedbahram
@hamedbahram 11 месяцев назад
Glad to hear that Hamza!
@saeidalidadi7579
@saeidalidadi7579 Год назад
Interesting and awesome Next.js video.
@hamedbahram
@hamedbahram Год назад
Thanks 👍
@CasualDrive
@CasualDrive 6 месяцев назад
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 6 месяцев назад
You can fetch data the user data on the server.
@AaronTMac
@AaronTMac Год назад
Damn this was beautifully explained.
@hamedbahram
@hamedbahram Год назад
Glad to hear that! Appreciate your comment 😃
@usamausman5225
@usamausman5225 Год назад
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: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-XmWGqi0pskU.html ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-8e35eo447Zw.html
@jayantsingh7768
@jayantsingh7768 6 месяцев назад
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 6 месяцев назад
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.
@udaym4204
@udaym4204 7 месяцев назад
Thank you Hamed 😃👍
@hamedbahram
@hamedbahram 7 месяцев назад
My pleasure 🙂
@m__link6499
@m__link6499 4 месяца назад
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 4 месяца назад
I'm not sure if I understand your question?!
@heysahilsingh
@heysahilsingh 10 месяцев назад
Really loving your channel ❤
@hamedbahram
@hamedbahram 10 месяцев назад
Glad to hear that!
@enafor99
@enafor99 Год назад
greate tutorial Ahmed , thanks so much👋❤
@hamedbahram
@hamedbahram Год назад
Thanks 🙏🏽
@heysahilsingh
@heysahilsingh 8 месяцев назад
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 8 месяцев назад
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.
@ukaszkunicki293
@ukaszkunicki293 Год назад
Great material, easy to understand and contains every important information.
@hamedbahram
@hamedbahram Год назад
Glad it was helpful! Appreciate your comment.
@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.
@amancharlamanas3812
@amancharlamanas3812 Год назад
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 Год назад
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.
@LuisCastroem
@LuisCastroem Год назад
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 Год назад
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.
@YotamAvraham
@YotamAvraham 7 месяцев назад
Brilliant video!
@hamedbahram
@hamedbahram 7 месяцев назад
Thank you!
@Shawn-Mosher
@Shawn-Mosher Год назад
Great topic and clearing up any confusion! Well done
@hamedbahram
@hamedbahram Год назад
Appreciate your comment Shawn.
@lemondigitaldesign
@lemondigitaldesign 11 месяцев назад
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 11 месяцев назад
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 11 месяцев назад
@@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 11 месяцев назад
@@lemondigitaldesign My pleasure! You're right there won't be a perfect solution, nor a one-size-fits-all.
@jobiej7416
@jobiej7416 Год назад
Thanks. How would you handle a “load more” scenario where the initial and subsequent data is rendered by a server component
@hamedbahram
@hamedbahram Год назад
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.
@shinobi_coder88
@shinobi_coder88 11 месяцев назад
11:13 Hi Hamed, what if we wanna pass props from Client to Server component? how can we do that?
@hamedbahram
@hamedbahram 11 месяцев назад
You can use the URL as a way to pass props to the server.
@aminsoraya
@aminsoraya 11 месяцев назад
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 11 месяцев назад
Yes you can using server actions. Look at the video I did on Infinite scrolling.
@aminsoraya
@aminsoraya 11 месяцев назад
yes you did it@@hamedbahram
@Adrian_Galilea
@Adrian_Galilea 8 месяцев назад
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 8 месяцев назад
Referring to this video, `How to Add Page Transitions in NextJs 14` also around 4:00
@Adrian_Galilea
@Adrian_Galilea 8 месяцев назад
Wait, I think you clarify it later on, still a bit confused.
@hamedbahram
@hamedbahram 8 месяцев назад
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 8 месяцев назад
@@hamedbahram That is perfect, thank you!
@dexcoder1207
@dexcoder1207 Год назад
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 Год назад
The button can be a client component that's rendered inside your page server components.
@dexcoder1207
@dexcoder1207 Год назад
@@hamedbahram thank you so much for your time
@hamedbahram
@hamedbahram Год назад
@@dexcoder1207 anytime!
@AlexSilva-ye2lp
@AlexSilva-ye2lp Год назад
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.
@qusaijabarisada
@qusaijabarisada 11 месяцев назад
Anyone else thinking this comes with a considerable development overhead compared to previous versions? FE development is becoming increasingly complicated
@hamedbahram
@hamedbahram 11 месяцев назад
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.
@satindersingh9671
@satindersingh9671 Год назад
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 Год назад
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 Год назад
I'll try make a little example project and share it with you. Thank you
@giovannitonussi3746
@giovannitonussi3746 Год назад
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 Год назад
@@giovannitonussi3746 yeah that'd be interesting 🤔
@igboanugwocollins4452
@igboanugwocollins4452 Год назад
Awesome video, learnt alot
@hamedbahram
@hamedbahram Год назад
Glad to hear it!
@rockNbrain
@rockNbrain Год назад
great job bro, tks a lot!
@hamedbahram
@hamedbahram Год назад
Thanks 🙏🏼
@un_chien_andalou
@un_chien_andalou Год назад
Thanks, man.
@hamedbahram
@hamedbahram Год назад
My pleasure!
@jean-pierrebanchereau8474
@jean-pierrebanchereau8474 Год назад
Very interesting study case. Thank's a lot
@hamedbahram
@hamedbahram Год назад
My pleasure! appreciate your comment.
@bogdanalexandru5071
@bogdanalexandru5071 11 месяцев назад
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 11 месяцев назад
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 11 месяцев назад
@@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 11 месяцев назад
@@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 11 месяцев назад
@@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 11 месяцев назад
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.
@BruceWayne-kw6xm
@BruceWayne-kw6xm 8 месяцев назад
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 8 месяцев назад
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 8 месяцев назад
@@hamedbahram thanks for the clarification.. will you make more videos on next js please
@hamedbahram
@hamedbahram 8 месяцев назад
@@BruceWayne-kw6xm you're welcome!
@oshoham1975
@oshoham1975 Год назад
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 Год назад
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 Год назад
@@hamedbahram Thanks, I figured it out, I applied a similar pattern to the 'providers' pattern that you demonstrated.
@hamedbahram
@hamedbahram Год назад
@@oshoham1975 sweet!
@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 Год назад
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 Год назад
@@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.
@SharifKhan-v8d
@SharifKhan-v8d Год назад
Please show us how to send emails forget password and more topics regarding next js
@hamedbahram
@hamedbahram Год назад
For sure! Appreciate your feedback.
@aroo1377
@aroo1377 Год назад
Does using use client causes SSR to break?
@hamedbahram
@hamedbahram Год назад
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.
@humanities1019
@humanities1019 5 месяцев назад
im comfused why the the styles changed even though you didn't put something like this dark:text-white
@hamedbahram
@hamedbahram 5 месяцев назад
I'm not sure either 😅
@raulparra4678
@raulparra4678 Год назад
Amazing explanation! Thanks.
@hamedbahram
@hamedbahram Год назад
Glad it was helpful! Appreciate you comment.
@ConnorBurge-dk5uj
@ConnorBurge-dk5uj 5 месяцев назад
What camera do you use?
@hamedbahram
@hamedbahram 5 месяцев назад
Sony zv-e10
@mehdinaji2389
@mehdinaji2389 Год назад
اقا حامد کار درست ❤❤
@hamedbahram
@hamedbahram Год назад
Eraadat 🫡
@MuwangaMohammed
@MuwangaMohammed Год назад
nice.. great teaching ...only synchronize your voice Sir
@hamedbahram
@hamedbahram Год назад
Thanks for the feedback
@joe_j
@joe_j Год назад
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. RU-vid is kinda deleting my replies
@joe_j
@joe_j Год назад
@@hamedbahram joe-jngigi/next_learn_dev
@Rehan-iw7jm
@Rehan-iw7jm Год назад
Hi Hamed, I already purchased your next.js course. can you please show the e-commerce project section video? Thank you.
@hamedbahram
@hamedbahram Год назад
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 Год назад
@@hamedbahram ok thank you for the update. Can you please inform me in email when the video is already up? Thank you.
@hamedbahram
@hamedbahram Год назад
@@Rehan-iw7jm For sure!
@Rehan-iw7jm
@Rehan-iw7jm Год назад
@@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 Год назад
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.
@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.
@hbela1000
@hbela1000 Год назад
top-notch,thx.
@hamedbahram
@hamedbahram Год назад
Appreciate your comment.
@hey.............
@hey............. Год назад
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 Год назад
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............. Год назад
​@@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 Год назад
@@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 Год назад
​@@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 Год назад
@@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 :)
@muhammahanisuzzaman5493
@muhammahanisuzzaman5493 Год назад
Jajakallah... Bro💯
@hamedbahram
@hamedbahram Год назад
Thank you.
@MajidAli-gx7gg
@MajidAli-gx7gg Месяц назад
cool
@hamedbahram
@hamedbahram Месяц назад
Thanks!
@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.
@mahesh-waghmare
@mahesh-waghmare Год назад
Amazing 🎉
@hamedbahram
@hamedbahram Год назад
Thank you! Cheers!
@imkir4n
@imkir4n Год назад
😀😀😀
@hamedbahram
@hamedbahram Год назад
🙏🏽
@AbdulRahim-zb9zm
@AbdulRahim-zb9zm 8 месяцев назад
Greaaaaaaaaaaaaaaat
@hamedbahram
@hamedbahram 8 месяцев назад
Thanks!
@AbdulRahim-zb9zm
@AbdulRahim-zb9zm 8 месяцев назад
@@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
@Grishopping
@Grishopping Год назад
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
@naderelmahdy
@naderelmahdy Год назад
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 Год назад
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.
Далее
Modern Data Fetching in React (Complete Guide)
16:41
Просмотров 48 тыс.
skibidi toilet 77 (part 3)
04:51
Просмотров 16 млн
10 common mistakes with the Next.js App Router
20:37
Просмотров 214 тыс.
Loading UI, Suspense, and Streaming in NextJs 13
19:43