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

  Рет қаралды 35,867

Hamed Bahram

Hamed Bahram

Күн бұрын

Пікірлер: 221
@SGZealotry
@SGZealotry 11 ай бұрын
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 11 ай бұрын
Thank you! I appreciate that. Im glad you found it helpful.
@mathiasriissorensen6994
@mathiasriissorensen6994 Жыл бұрын
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 Жыл бұрын
Thanks for sharing. I'm glad to hear that. Welcome onboard!
@Isaackasongoyo
@Isaackasongoyo 8 ай бұрын
I just read everything I needed about nextJS new direction in just less than 35 min and it worth it. Great content
@hamedbahram
@hamedbahram 8 ай бұрын
Glad to hear you found it helpful.
@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 Жыл бұрын
i had same issue bro, so true!
@erenyeager_0510
@erenyeager_0510 Жыл бұрын
wow wow wow!!!! you have no idea how much information and key points you provided in just 34 minutes. thanks a lot😇🤩
@hamedbahram
@hamedbahram Жыл бұрын
Thank you! I appreciate your comment. Glad you found it helpful.
@goku_watches
@goku_watches Жыл бұрын
Excellent explanation. Just delving into NextJs, wanted a clear explanation, and you've successfully provided one. Thank you
@hamedbahram
@hamedbahram Жыл бұрын
Glad to hear that! Welcome to the channel 🙂
@yashpandey350
@yashpandey350 Жыл бұрын
2:10 So finally I understood the real thing behind the madness behind adopting nextjs❣❣
@hamedbahram
@hamedbahram Жыл бұрын
Awesome! Glad to hear that...
@albertoplebani77
@albertoplebani77 Жыл бұрын
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 Жыл бұрын
Great to hear that! I appreciate it.
@Claudia-hz4ly
@Claudia-hz4ly 11 ай бұрын
I loved the way this is explained! You made this so simple to understand! Thank you so much!
@hamedbahram
@hamedbahram 11 ай бұрын
You're very welcome! Glad it was helpful!
@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.
@Petyr25
@Petyr25 Жыл бұрын
This is the video everyone should watch. Absolutely incredible!
@hamedbahram
@hamedbahram Жыл бұрын
Thanks! I appreciate your comment.
@felixzepol
@felixzepol Жыл бұрын
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.
@li-anchen9768
@li-anchen9768 8 ай бұрын
Thank you for the very clear explanation and concrete example which gives me a better grasp of Next.js!
@hamedbahram
@hamedbahram 8 ай бұрын
My pleasure! 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!
@abhilashkumar4753
@abhilashkumar4753 Жыл бұрын
Incredible, clear explanation of concepts 😮
@hamedbahram
@hamedbahram Жыл бұрын
Thanks! I appreciate it!
@thehamzajunaid
@thehamzajunaid Жыл бұрын
very informative Hamed. Thanks for this.
@hamedbahram
@hamedbahram Жыл бұрын
Glad to hear that Hamza!
@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.
@patrickc.2680
@patrickc.2680 Жыл бұрын
Extremely helpful video, I just subscribed to this channel.
@hamedbahram
@hamedbahram Жыл бұрын
Welcome aboard!
@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.
@naveedalirehmani4135
@naveedalirehmani4135 Жыл бұрын
great video exactly what I was looking for!
@hamedbahram
@hamedbahram Жыл бұрын
Great to hear!
@imkir4n
@imkir4n Жыл бұрын
This video gives me so much insight about server components👍
@hamedbahram
@hamedbahram Жыл бұрын
Appreciate your comment!
@cplus4
@cplus4 Жыл бұрын
I stumbled across your channel this week and have started binge watching everything. This is by far the best NextJS content on KZbin. 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.
@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.
@heysahilsingh
@heysahilsingh Жыл бұрын
Really loving your channel ❤
@hamedbahram
@hamedbahram Жыл бұрын
Glad to hear that!
@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!
@saeidalidadi7579
@saeidalidadi7579 Жыл бұрын
Interesting and awesome Next.js video.
@hamedbahram
@hamedbahram Жыл бұрын
Thanks 👍
@ukaszkunicki293
@ukaszkunicki293 Жыл бұрын
Great material, easy to understand and contains every important information.
@hamedbahram
@hamedbahram Жыл бұрын
Glad it was helpful! Appreciate your comment.
@Shawn-Mosher
@Shawn-Mosher Жыл бұрын
Great topic and clearing up any confusion! Well done
@hamedbahram
@hamedbahram Жыл бұрын
Appreciate your comment Shawn.
@udaym4204
@udaym4204 11 ай бұрын
Thank you Hamed 😃👍
@hamedbahram
@hamedbahram 11 ай бұрын
My pleasure 🙂
@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.
@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 🙂
@AaronTMac
@AaronTMac Жыл бұрын
Damn this was beautifully explained.
@hamedbahram
@hamedbahram Жыл бұрын
Glad to hear that! Appreciate your comment 😃
@Adrian_Galilea
@Adrian_Galilea Жыл бұрын
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 Жыл бұрын
Referring to this video, `How to Add Page Transitions in NextJs 14` also around 4:00
@Adrian_Galilea
@Adrian_Galilea Жыл бұрын
Wait, I think you clarify it later on, still a bit confused.
@hamedbahram
@hamedbahram Жыл бұрын
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 Жыл бұрын
@@hamedbahram That is perfect, thank you!
@enafor99
@enafor99 Жыл бұрын
greate tutorial Ahmed , thanks so much👋❤
@hamedbahram
@hamedbahram Жыл бұрын
Thanks 🙏🏽
@soaringeaglet4956
@soaringeaglet4956 Жыл бұрын
Great resource, I hope you blow up! weekend binge watch material.
@hamedbahram
@hamedbahram Жыл бұрын
Thanks, I appreciate it!
@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 😂
@raulparra4678
@raulparra4678 Жыл бұрын
Amazing explanation! Thanks.
@hamedbahram
@hamedbahram Жыл бұрын
Glad it was helpful! Appreciate you comment.
@jean-pierrebanchereau8474
@jean-pierrebanchereau8474 Жыл бұрын
Very interesting study case. Thank's a lot
@hamedbahram
@hamedbahram Жыл бұрын
My pleasure! appreciate your comment.
@YotamAvraham
@YotamAvraham 10 ай бұрын
Brilliant video!
@hamedbahram
@hamedbahram 10 ай бұрын
Thank you!
@ahmetcanisik
@ahmetcanisik 8 ай бұрын
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 8 ай бұрын
Glad it helped!
@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.
@zainsyed9811
@zainsyed9811 5 күн бұрын
Great video. Just one thing instead using useEffect could we just do const { resolvedTheme, setTheme } = useTheme() if (!resolvedTheme) return null as it only resolves once it's mounted. Seems to work fine on testing.
@hamedbahram
@hamedbahram 3 күн бұрын
The useEffect is from the next-themes documentation.
@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.
@igboanugwocollins4452
@igboanugwocollins4452 Жыл бұрын
Awesome video, learnt alot
@hamedbahram
@hamedbahram Жыл бұрын
Glad to hear it!
@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.
@shinobi_coder88
@shinobi_coder88 Жыл бұрын
11:13 Hi Hamed, what if we wanna pass props from Client to Server component? how can we do that?
@hamedbahram
@hamedbahram Жыл бұрын
You can use the URL as a way to pass props to the server.
@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!
@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: kzbin.info/www/bejne/jp66eKSfZdWmobc kzbin.info/www/bejne/bpaWZpilaZlqkNk
@CasualDrive
@CasualDrive 10 ай бұрын
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 10 ай бұрын
You can fetch data the user data on the server.
@rockNbrain
@rockNbrain Жыл бұрын
great job bro, tks a lot!
@hamedbahram
@hamedbahram Жыл бұрын
Thanks 🙏🏼
@muhammahanisuzzaman5493
@muhammahanisuzzaman5493 Жыл бұрын
Jajakallah... Bro💯
@hamedbahram
@hamedbahram Жыл бұрын
Thank you.
@mehdinaji67
@mehdinaji67 Жыл бұрын
اقا حامد کار درست ❤❤
@hamedbahram
@hamedbahram Жыл бұрын
Eraadat 🫡
@lemondigitaldesign
@lemondigitaldesign Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
@@lemondigitaldesign My pleasure! You're right there won't be a perfect solution, nor a one-size-fits-all.
@un_chien_andalou
@un_chien_andalou Жыл бұрын
Thanks, man.
@hamedbahram
@hamedbahram Жыл бұрын
My pleasure!
@bogdanalexandru5071
@bogdanalexandru5071 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
@@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 Жыл бұрын
@@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 Жыл бұрын
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.
@jayantsingh7768
@jayantsingh7768 9 ай бұрын
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 9 ай бұрын
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.
@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.
@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.
@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.
@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 🤔
@m__link6499
@m__link6499 7 ай бұрын
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 7 ай бұрын
I'm not sure if I understand your question?!
@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.
@heysahilsingh
@heysahilsingh Жыл бұрын
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 Жыл бұрын
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.
@diegofss11
@diegofss11 2 ай бұрын
“Next.js would cache and deduplicate repeated requests”. Do you know how that would work in a scenario where multiple Node.js servers are behind a load balancer using round-robin distribution?
@hamedbahram
@hamedbahram 2 ай бұрын
The request deduplication mentioned in this video is a short-lived per-request cache that does not span across other servers / requests.
@ConnorBurge-dk5uj
@ConnorBurge-dk5uj 9 ай бұрын
What camera do you use?
@hamedbahram
@hamedbahram 9 ай бұрын
Sony zv-e10
@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.
@aminsoraya
@aminsoraya Жыл бұрын
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 Жыл бұрын
Yes you can using server actions. Look at the video I did on Infinite scrolling.
@aminsoraya
@aminsoraya Жыл бұрын
yes you did it@@hamedbahram
@hbela1000
@hbela1000 Жыл бұрын
top-notch,thx.
@hamedbahram
@hamedbahram Жыл бұрын
Appreciate your comment.
@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. KZbin is kinda deleting my replies
@joe_j
@joe_j Жыл бұрын
@@hamedbahram joe-jngigi/next_learn_dev
@mahesh-waghmare
@mahesh-waghmare Жыл бұрын
Amazing 🎉
@hamedbahram
@hamedbahram Жыл бұрын
Thank you! Cheers!
@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!
@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.
@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
@imkir4n
@imkir4n Жыл бұрын
😀😀😀
@hamedbahram
@hamedbahram Жыл бұрын
🙏🏽
@BruceWayne-kw6xm
@BruceWayne-kw6xm Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@hamedbahram thanks for the clarification.. will you make more videos on next js please
@hamedbahram
@hamedbahram Жыл бұрын
@@BruceWayne-kw6xm you're welcome!
@qusaijabarisada
@qusaijabarisada Жыл бұрын
Anyone else thinking this comes with a considerable development overhead compared to previous versions? FE development is becoming increasingly complicated
@hamedbahram
@hamedbahram Жыл бұрын
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.
@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
@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.
@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.
@MuwangaMohammed
@MuwangaMohammed Жыл бұрын
nice.. great teaching ...only synchronize your voice Sir
@hamedbahram
@hamedbahram Жыл бұрын
Thanks for the feedback
@humanities1019
@humanities1019 9 ай бұрын
im comfused why the the styles changed even though you didn't put something like this dark:text-white
@hamedbahram
@hamedbahram 9 ай бұрын
I'm not sure either 😅
@MajidAli-gx7gg
@MajidAli-gx7gg 4 ай бұрын
cool
@hamedbahram
@hamedbahram 4 ай бұрын
Thanks!
@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 :)
@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.
@AbdulRahim-zb9zm
@AbdulRahim-zb9zm Жыл бұрын
Greaaaaaaaaaaaaaaat
@hamedbahram
@hamedbahram Жыл бұрын
Thanks!
@AbdulRahim-zb9zm
@AbdulRahim-zb9zm Жыл бұрын
@@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
Next.js App Router REVIEW (Six Months In Prod)
16:10
Theo - t3․gg
Рет қаралды 61 М.
Dark Theme in NextJs 13 - Using React Context in Server Components
19:57
Why I don't use React-Query and tRPC in Next.js
18:58
ByteGrad
Рет қаралды 102 М.
Monolithic vs Microservice Architecture: Which To Use and When?
10:43
Self-Hosting Next.js
45:13
leerob
Рет қаралды 82 М.
10 common mistakes with the Next.js App Router
20:37
Vercel
Рет қаралды 239 М.
You’re leaking data with Server Actions in Next.js (accidentally)
14:54
Server Components in React (Simple Tutorial)
25:27
Cosden Solutions
Рет қаралды 23 М.
Next.js Server Actions...  5 awesome things you can do
7:51
Beyond Fireship
Рет қаралды 287 М.