Must Know App Router Secret: Promises For Props

  Рет қаралды 27,302

Jack Herrington

Jack Herrington

Күн бұрын

👉 Upcoming NextJS course: pronextjs.dev
👉 Code : github.com/jherr/rsc-use
👉 Don't forget to subscribe to this channel for more updates: bit.ly/2E7drfJ
👉 Discord server signup: / discord
👉 VS Code theme and font? Night Wolf [black] and Operator Mono
👉 Terminal Theme and font? oh-my-posh with powerlevel10k_rainbow and SpaceMono NF
0:00 Introduction
1:02 Project Setup
2:37 Sending Data Via Props
5:15 Passing Promises as Props
8:02 Sharing Promises With Context
9:54 Error Handling
11:15 Streaming With Suspense
12:19 Using React-Query or SWR
15:13 Outroduction

Пікірлер: 176
@techs7296
@techs7296 11 ай бұрын
I didnt knew this but i faced this performance issue. Thanks Jack for help.
@viper_eyes
@viper_eyes 11 ай бұрын
As always ,you are awesome Jack, you keep digging here and there and come out with something incredible like this , and I see the beard coming ,cheers man and tell the wife to let the beard grow
@developerpranav
@developerpranav 10 ай бұрын
This is the most promising data-fetching and handling video I've seen regarding the new app router! Definitely signing up for Pro NextJS!
@brucegenerator2755
@brucegenerator2755 4 ай бұрын
jack you absolutely rule! so much of your content helps me at work and quite often; whether its brainstorming, understanding new concepts, or an idea of a new direction to look into. thanks so much and keep it up man!
@tonystark4416
@tonystark4416 11 ай бұрын
Thanks! Relatively new React web dev here, and I just wanted to say I am enjoying your lesson
@ollierkul
@ollierkul 11 ай бұрын
Literally met an issue today that this seems to solve, perfect timing!
@user-db1ue8om7v
@user-db1ue8om7v 10 ай бұрын
What a great video! So easy to understand and see the power of this pattern. Thanks Jack!
@samisaacvicliph
@samisaacvicliph 3 ай бұрын
What an absolute gem! You made it so easy to understand even for a beginner like me!
@1felixxex1
@1felixxex1 11 ай бұрын
Nice vid! Bonus that I learned about the underscore idea for naming folders... I went back to the docs to read more about how that works.
@PostMeridianLyf
@PostMeridianLyf 11 ай бұрын
Thank you so much. Ive been building all my full stack next apps without using the Api functionality in next. Now I am definitely going to use it.
@andrewc8125
@andrewc8125 11 ай бұрын
Wow...amazing as always.
@kengreeff
@kengreeff 11 ай бұрын
Nice work Jack!
@kumrogames
@kumrogames 11 ай бұрын
Very cool! Great info as always sir!
@shin5302
@shin5302 11 ай бұрын
Thank you for these fantastic tips.
@joeyywill1234
@joeyywill1234 11 ай бұрын
Great vid again jack :)
@meka4996
@meka4996 10 ай бұрын
Super man!
@echobucket
@echobucket 10 ай бұрын
I'm constantly shocked by how complicated all of this has gotten.
@basheerberkdar5238
@basheerberkdar5238 11 ай бұрын
Always special one for me ❤️
@CFXTBogard
@CFXTBogard 11 ай бұрын
Nice, this content is so high I'm throwing away my old gummies.
@jherr
@jherr 10 ай бұрын
Yeah, don't throw away the new gummies. :)
@admiralicic
@admiralicic 10 ай бұрын
Awesome 👍
@xerossx
@xerossx 10 ай бұрын
Love it, thank you!
@JEM_GG
@JEM_GG 11 ай бұрын
had to do a little reading on use() but this is looking really really cool
@Honey_cam12
@Honey_cam12 10 ай бұрын
Awesome!
@8koi245
@8koi245 10 ай бұрын
insta-subbed to the newsletter lol
@umargulzar2982
@umargulzar2982 11 ай бұрын
Next Js Guru ❤❤❤ Love from Pakistan ❤❤❤
@realitydesigners
@realitydesigners 11 ай бұрын
This helped alot thanks
@avneet12284
@avneet12284 11 ай бұрын
Excellent
@DanielEmenikeCodes
@DanielEmenikeCodes 10 ай бұрын
Awesome video. I actually use RTK and RTK query for my projects and hoping to move them into NextJs. Request: Please Can you make a video on using RTK with Next JS . I will be grateful.
@rahmonpro381
@rahmonpro381 10 ай бұрын
thanks very useful 👍
@SajidAliSoftwareEngineer
@SajidAliSoftwareEngineer 10 ай бұрын
Thank you for the nice content. a request, can you please share your environment setup (Terminal with Theme/VSCode theme/fonts)
@chadborghinibeats8675
@chadborghinibeats8675 10 ай бұрын
Hey love the videos and ive been subbed for a while. Could you please make a video regarding csrf protection in the new app router dir. There seems to be no packages for the new update and there really isn't many videos covering how to create your own either.
@WilliamShrek
@WilliamShrek 11 ай бұрын
Oh my god. You just know every aspect of Next.js.
@zackinfos5933
@zackinfos5933 10 ай бұрын
Thank you infinitely for the efoort and the knowledge you are spreading with us. May i forward my question or a request let's say... What is the best way to connecr nextjs and a separate backend in term of forming our logic, there are actually ways for example request direct from components or using api routes as a proxy
@kassios
@kassios 7 ай бұрын
Thanks
@ThomazMartinez
@ThomazMartinez 11 ай бұрын
What keyboard you use? sounds nice
@underflowexception
@underflowexception 11 ай бұрын
I would be interested to see you make a video exploring Livewire 3. I don't think it gets enough attention from the tech community here on KZfaq considering what it's capable of doing.
@jherr
@jherr 11 ай бұрын
I do like Livewire, but I'll probably do htmx first because it's hot now (gotta love the YT algorithm) and it has that server-driven mechanic. :)
@username4794
@username4794 11 ай бұрын
Pro tip: when creating files in VSCode you can write the file path. Vs code will automatically create all folders to that file. Found that feature wery useful when defining routes in app router 🙂
@jherr
@jherr 11 ай бұрын
That is an awesome tip! Thanks!
@afion113
@afion113 10 ай бұрын
Thank you so much for explaining this Jack! When passing promises as props, does it mean the 'promise cracking' phrase is happening in React hydration stage? Does it also mean it might affect the CLS if I do not have the right size (dimensions) for a loading component as fallback? Thank you so much again for these awesome video!
@jherr
@jherr 10 ай бұрын
`use` is cracking the promise and most likely throwing the promise on render and then React is getting that promise and subscribing to it itself so that when it resolves it will re-render the component. And all that is done on the server side. If you look at the RSC payload you can see a "promise type" marker with the data from the promise, so it's resolved on the server. Then on the client during hydation they use do a Promise.resolve(payload) and pass that in.
@afion113
@afion113 10 ай бұрын
@@jherr Hi Jack, Thank you for explaining how the promise works both is RSC and client component! If everything rendered on the server side, would it be the same as SSR in page route and the page load should be fast? I thought I would not even see the loading stage when the html is ready to be served. However when I tried your example with image fetch instead, I would see a loading state before image loaded, rather than seeing the image right away (comparing to page route approach). Does it mean it is slow even when the promise gets resolved on the server?
@jherr
@jherr 10 ай бұрын
@@afion113 So if you are going to get the image data on the server, and that image API is slow, then ask yourself, would you rather the customer have an interactive page with a spinner, or a page that is slow to respond? Because the App Router gives you the ability to have a fully interactive page where parts of it stream and become interactive. Where the pages router simply blocks on getServerSideProps which means that the page would simply be slow to repond.
@SchokoMuffinMhmhm
@SchokoMuffinMhmhm 10 ай бұрын
Is there a particular reason you reached for react-error-boundary instead of the build in error.js file from Nextjs?
@ollierkul
@ollierkul 11 ай бұрын
wooo
@jiachen1078
@jiachen1078 10 ай бұрын
great video! thank you! somehow, after i change to use ReactQuery, the html in the client side component doesn't show up in the html dom. any idea what i might do wrong?
@will_zzy
@will_zzy 8 ай бұрын
Hi, Jack. I enjoyed this video a lot! I have one question though. In the documentation of react-error-boundary, the author says it can only be used in a client component. Why does it works fine in a server component in this case?
@jherr
@jherr 8 ай бұрын
Server compents can include client components. And client components can include server components and children or rendered props.
@nilsandresen97
@nilsandresen97 10 ай бұрын
Would you say that the use hook is production ready? All documentation I could find about it is in the React RFC. 😥
@nabinsaud4688
@nabinsaud4688 11 ай бұрын
I need a robust machine like your
@Getentertainedp
@Getentertainedp 11 ай бұрын
Awesome video. How do you get those long accurate suggestions?
@jherr
@jherr 11 ай бұрын
Copy and paste from a previously implemented correct version. I really don't see the need to make you watch me poorly type stuff. :)
@Getentertainedp
@Getentertainedp 11 ай бұрын
@@jherr oh I see. Anyway nice video. I learn a lot of new stuff from you. Thanks man.
@VincentFulco
@VincentFulco 11 ай бұрын
Hey Jack. Huge fan of your stuff. Tried to sign up to the newsletter 3x and I get no response from your ESP. Using fastmail and not in spam. thx
@jherr
@jherr 11 ай бұрын
You can send a request directly to team@pronextjs.dev to get registered. The site is still new and there were some issues with the certificates.
@Cahnisama
@Cahnisama 11 ай бұрын
Interested in the app Router nextjs course. I am just hoping there is purchase power parity, brazillian real is super weak and my Jr salary doesn't go very far
@officialrajdeepsingh
@officialrajdeepsingh 10 ай бұрын
👍👍👍👍👍
@DenisMitiliuc
@DenisMitiliuc 11 ай бұрын
Hello Jack thank you for your awesome videos. I wanted to ask you a question that I’m struggling for a while. I have two projects one is a nextjs project of a website and the second one is a monorepo project that contains reusable components. These components are published to npmjs and are imported as dependency into the first one. The issue is in the development workflow. I’m trying to link with pnpm link the components packages to ease the workflow and it works but if you have any hook for example useState in any component it will throw an error saying can not read useState of undefined. Thank you
@jherr
@jherr 11 ай бұрын
You probably have competing React versions. So the components are on one version of React and the NextJS server is another. Is this 13? If so you want 18 across the board and probably more specific than that. FWIW, the best solve here is to bring everything into the monorepo.
@DenisMitiliuc
@DenisMitiliuc 10 ай бұрын
@@jherr yes very good point and I checked it all projects are of the same version of react and common dependencies. You can easily test it by recreating two new projects from scratch (we tested it too). One strange thing is that when we where using nextjs 12 this issue was not present after upgrading it to 13 it appeared. I might be wrong but I think it is related to new transpile option in nextjs 13. Before we were using the withTM module.
@sinkie420
@sinkie420 10 ай бұрын
I found out that server actions are now fully usable in both server and client components and have replaced all of my api routes with those. Integrates amazingly with tanstack query too and its much easier to write them. Is there any downside to this?
@curufinw3
@curufinw3 10 ай бұрын
I found using server actions for data fetching to have some important limitations. For me the deal breaker was having no built-in way to abort requests, like you would using an AbortController on normal fetch calls. This is a big issue with typeahead server validation or autocomplete requests, where promises may be resolved out of order
@bioburden
@bioburden 10 ай бұрын
That's quite a bit of extra boilerplate and complexity just to trim the fat from the v-DOM.
@Thorax232
@Thorax232 11 ай бұрын
I took a break from this channel for a bit, I can't believe you're still talking about NextJS payloads, it's seems like every single video. Using context for promises is a decent pattern but not realistic in the long term. And to make use of Suspense with RQ, it has a flag for that. Just using isFetching by itself is a CSR indicator. And not likely what you want if you want that loading state to float up to a larger component.
@anhvuuc8693
@anhvuuc8693 10 ай бұрын
I like you video edit, it's so details. I would love to learn your. Can you please make a course on Udemy?
@moosegoose1282
@moosegoose1282 10 ай бұрын
whats that terminal u are using to view webpage?
@snehaldilipbhaipatel1421
@snehaldilipbhaipatel1421 11 ай бұрын
Great video, I am not sure how React query is able to stream HTML for loading indicator first and then foods later without using Suspense. I thought Suspense is the only mechanism to tell React that a certain part of the application is waiting for data.
@jherr
@jherr 11 ай бұрын
I need to investigate a little further how RQ is doing this.
@r0mankon
@r0mankon 11 ай бұрын
I don't think streaming has anything to do with client component. It's fetching and rendering UI's after reaching the browser!
@snehaldilipbhaipatel1421
@snehaldilipbhaipatel1421 11 ай бұрын
Does this mean the loading indicator for RQ is replaced with foods by client side bundle. It is not same rendering pattern as React streaming using Suspense ?@@r0mankon
@jherr
@jherr 11 ай бұрын
@@r0mankon have a look at what’s coming out of the server. It’s holding the connection open then sending more HTML and JS as the streams are resolved. That’s not initiated by the client.
@r0mankon
@r0mankon 11 ай бұрын
@@jherr Interesting how that might work! Let's wait for your findings.
@DesolationPhoenix
@DesolationPhoenix 11 ай бұрын
Hi Jack! Do you have any references for understanding how client components that receive props from server components are rendered? It’s confusing to me that a client component that receives props from a server component would actually be server-rendered and “blocked” by the fetching in the server component, as you mentioned. What parts of such a client component are actually client-rendered then? Thanks!
@jherr
@jherr 11 ай бұрын
The actual blocking in the "client" component happens on the server side, the rendered component is sent to the client when the server render of the client component is completed. "Client" components are rendered on both the client and the server.
@DesolationPhoenix
@DesolationPhoenix 11 ай бұрын
@@jherr In that case, is there a way to render a loading state in place of the client component while the server component is performing the data fetching? Should I use a loading.tsx? Thank you so much!
@jherr
@jherr 11 ай бұрын
@@DesolationPhoenix Yeah, if you use the `use` hook then wrap it in a Suspense as shown in the video. If you use the React-Query `useQuery` hook then look at the `isFetching` boolean and render a loading state if it's true (again, as shown in the video.)
@DesolationPhoenix
@DesolationPhoenix 11 ай бұрын
@@jherrthank you so much!!!
@maltehecht1845
@maltehecht1845 11 ай бұрын
That question was in my head the whole time while watching the video 😅 Honestly, the way that is handled with rsc is next level. That's so much more convenient. Btw. are the any good reasons for using react query etc over use + suspense?
@PUTTEKOOL
@PUTTEKOOL 10 ай бұрын
Don't you still feel we are introducing many anti patterns for server side components here? 1.Creating a api endpoint for fetch that should be done in the server component not an own api 2.Talking about React Query and such that becomes more obsolete with server side fetching and caching. 3.Using boundary package. The thing for me is that I have not come to face this kind of problem, maybe I will, but still this feels too complex.
@osmanakar2507
@osmanakar2507 11 ай бұрын
great video 🙌 I do not understand why we had multiple copy of the data in vdom part 4:47 . what is the cause of this? data is already at page level and in one place. 😕
@jherr
@jherr 11 ай бұрын
Anything you pass to a client component as a property is serialized and serialized independently of any other component. That's why the multiple copies. The context is a single copy because we are only sending the prop to the provider client component and then it's sharing it around both on the server and on the client.
@osmanakar2507
@osmanakar2507 11 ай бұрын
thanks for the answer 🙌 I believe some kind of lint rule is needed to avoid this. It is a mistake that a developer can easily make and it can realy hurt the perf. was it like this before app router?
@RobbieDds
@RobbieDds 10 ай бұрын
What is the name of extension where is counted component renders?
@grif.n
@grif.n 10 ай бұрын
Will react-query still re-fetch on invalidation in this example? Is it aware of how to make the fetch happen again if it is passed as a prop from server component?
@jherr
@jherr 10 ай бұрын
No, it will simply re-request the original promise which will return the original data.
@SharunKumar
@SharunKumar 2 ай бұрын
How to get those console looks inline in the ide? 👀
@Glorion666
@Glorion666 11 ай бұрын
What is the "use" hook that you're importing from "react" on 12:33 ? Because i can't find any information about it in React docs. Thanks!
@jherr
@jherr 11 ай бұрын
It's in the experimental build of React that the App Router uses.
@mountakhabi
@mountakhabi 9 ай бұрын
@@jherr but its not production ready and NextJS don't recommend it in client. What is the alternative ?
@abhilashm5236
@abhilashm5236 10 ай бұрын
Can you please do a video on how to do a PWA using Next JS 13 ( with app router ).
@minhhieu1896
@minhhieu1896 10 ай бұрын
Nice, but is there anyway to make RSC to refetch?
@rico5146
@rico5146 9 ай бұрын
try to use router.refresh on the client component or revalidatePath on api route
@tameem551
@tameem551 11 ай бұрын
If we don't include "no-cache" then we wouldn't be "awaiting" for the data in all the components and the components would just reuse the same promise. And I reckon if we use context for passing the actual data instead of the promise, it should also send only one copy of the data with the vdom
@jherr
@jherr 11 ай бұрын
If you a requesting off the client then yeah, they would all pick up the same cache. But that's not going to SSR, so if you want SSR then you have to request the data in an RSC.
@AmanKumar-eg5rc
@AmanKumar-eg5rc 8 ай бұрын
Which vs code extension are you using in which data is render on vs code itself 6:53 timestamp?It is really cool...
@SharunKumar
@SharunKumar 2 ай бұрын
I was thinking the same thing, did you happen to find it?
@AmanKumar-eg5rc
@AmanKumar-eg5rc 2 ай бұрын
@@SharunKumar console ninja
@Nurof3n_
@Nurof3n_ 10 ай бұрын
console ninja slaps!
@rodolfosolano2229
@rodolfosolano2229 11 ай бұрын
Mr Jack set localhost as endpoint, but if we are working with real life project, how do we set the endpoint?
@jherr
@jherr 11 ай бұрын
My recommendation is to use middleware to inject an API header key into the request header with the prefix for the API. It might be something like `X-API-Base-URL` or something like that, which you can set however you want in the middleware and then the components pick it up with `headers`.
@dd-gk3mv
@dd-gk3mv 11 ай бұрын
What vsc's theme do you use?
@jherr
@jherr 11 ай бұрын
Night Wolf [black]
@dd-gk3mv
@dd-gk3mv 10 ай бұрын
@@jherr Thank you so much!
@herolooter3868
@herolooter3868 9 ай бұрын
Next js 13 loading hooks very slow when page loaded then its loading hooks and javascript example my sidebar loaded its need hooks to works it take 2s to load hooks when page full loaded my web app look like its lagging
@Andreiu2011
@Andreiu2011 11 ай бұрын
I'm not sure I understand the benefit of react server components if you're doing promises for props. Like what's even the point then? Seems like you're running in circle by doing this whole shazam, using tanstack query on the frontend would've provided the exact same benefit
@jherr
@jherr 11 ай бұрын
A big benefit is that you are making the request off the server. So you get better security and performance. In addition if you need SEO then there is a big benefit to rendering on the server because the HTML is generated on the server and is easier for the bots to handle.
@Andreiu2011
@Andreiu2011 11 ай бұрын
@@jherr I'm talking about this specific use case in the video (promises for props). Performance would be the same since you wait for the promise to finish. I'm not sure about the security part, what security issues does this prevent that a separate backend wouldn't? The other flow (with tanstack query for example) would be to fetch from the client from a backend server, then that server can fetch the same way it's being done in the video, only less convenient I guess. And I might be wrong on this, but server components don't generate HTML on the server (that's being done by renderToString) but instead send the serialized react tree to the client (so not *directly* html)
@jherr
@jherr 11 ай бұрын
@@Andreiu2011 So about whether the API is being called from the server or the client. There are several reasons why it's ideal to call from the server. First, NextJS servers are often deployed in the same VPC as the microservices they call, so it's a much faster connection. Second, the microservices don't need to be deployed to the open Internet because they are being called only by the NextJS servers within the VPC. And third, third party APIs often have access keys which shouldn't be sent to the client for security reasons. So making the requests from the server avoids the key leakage. There are more advantages, but those are some of the primary ones. And "server components don't generate HTML on the server" is pretty easy to test that yourself, just create a NextJS app, start it up and look at the page source in the browser. You will see all the rendered HTML (as well as the VDOM). "View Page Source" shows what comes back from the server, it does not show the current tree. For the current tree you can use the inspector. You can also see the HTML rendered from the server in the Network panel of the inspector as the response to the initial route request. Or you can just curl the server as see what comes back.
@MrPlaiedes
@MrPlaiedes 10 ай бұрын
If you're scaffolding to use app router but grab the football and instead pass the promise to a client component, why not just have the client component fetch with React Query to begin with?
@swartex92
@swartex92 11 ай бұрын
what theme are you using in vscode ?
@jherr
@jherr 11 ай бұрын
Night Wolf [black]
@swartex92
@swartex92 11 ай бұрын
@@jherr thanks 👍
@OmidFaryabi
@OmidFaryabi 11 ай бұрын
Can we use the error.js and loading.js and not installl additional packages?
@jherr
@jherr 11 ай бұрын
I only brought in the external library in the error case for simplicity. An error boundary is a single class component that is ~10 lines of code. But yeah, if you just want a catch-all you can do error.tsx.
@codewithalade5399
@codewithalade5399 11 ай бұрын
Good content. But it's weird to create context for each entity in my app.
@jherr
@jherr 11 ай бұрын
Then have a single context that contains multiple promises.
@rubendacostaesilva8442
@rubendacostaesilva8442 10 ай бұрын
@@jherr instead of using context, could we use Zustand or Jotai, to achieve the same thing?
@jherr
@jherr 10 ай бұрын
@@rubendacostaesilva8442 I wouldn't use Zustand or Jotai in their default "global" mode since globals with App Router are a no-no. Instead I would provide Zustand or Jotai atoms via context so that you are guaranteed that the state managers are per-request. Zustand and Jotai totally support this model, it's just not as super-easy as it usually is. For Zustand for example, you first call a hook that gets the Zustand hook from the context, and then you call the Zustand hook just like before.
@rubendacostaesilva8442
@rubendacostaesilva8442 10 ай бұрын
@@jherr that's sound kind of "gimmickly", but I'm going to give it a try. Thanks for the response, and keep up the great work you're doing!
@Ga2-20
@Ga2-20 10 ай бұрын
What about SEO in this kind of usage? Is it going to be affected? :)
@jherr
@jherr 10 ай бұрын
No. Streaming doesn’t effect SEO.
@Ga2-20
@Ga2-20 10 ай бұрын
@@jherr thanks :)
@shadowfaxenator
@shadowfaxenator 11 ай бұрын
The problem that now error handling is not progressively enhanced. You need JS to handle them
@jherr
@jherr 11 ай бұрын
I don't think that's correct. All of the action is happening on the server. So client-side JS or not you would still get the error boundary content (or error.tsx) content coming off the server if there was an error.
@shadowfaxenator
@shadowfaxenator 11 ай бұрын
@@jherr try to turn off js, you wont get any errors to render. In remix you will. Dan Abramov, explained that there was a missing part for sending errors as an action return. And it’s not implemenred
@jherr
@jherr 11 ай бұрын
@@shadowfaxenator You are correct sir. Thanks for the heads up on that one.
@rajeshdavide
@rajeshdavide 11 ай бұрын
If we are using Suspense, then I assume even if you pass a promise or await a promise on the server, the user would not notice a difference right ?
@jherr
@jherr 11 ай бұрын
It depends on what else you have going on. The await on the server blocks any other components that are also loading.
@rajeshdavide
@rajeshdavide 11 ай бұрын
@@jherr Alright. So essentially you await on the client side using "use" hook instead of the server ! Right ?
@jherr
@jherr 11 ай бұрын
@@rajeshdavide That's actually pretty complicated because it's both on the client and the server. "client" components run on BOTH the client and the server. That's number one. So when a "client" component on the server that uses `use` gets a fulfilled promise it renders on the SERVER. The HTML and the data are then both sent to the client where the component instance on the client is hydrated symmetrically and the `use` is run there too.
@rajeshdavide
@rajeshdavide 11 ай бұрын
So if that's the case, can I assume that the fullfilled promise is streamed after the client renders the component ? Do you have a material where I can read more ?
@rajeshdavide
@rajeshdavide 11 ай бұрын
In my mental model use hook was akin to await, if the client component that uses "use" runs on the server will it not wait until promise is resolved ?
@abdellahalaoui6609
@abdellahalaoui6609 11 ай бұрын
I can't understand why you didn't use loading.tsx and error.tsx to handle the loading and error state instead of using suspense and errorboundary
@jherr
@jherr 11 ай бұрын
Loading and error are great to handle the route level errors. Here I'm demonstrating how to manage errors and suspenses at whatever level of granularity you want within the component tree.
@dmitriykapeliushnii9017
@dmitriykapeliushnii9017 11 ай бұрын
The page source code contains "Foods: Pizza, Burger, Sandwich, Pasta", but not RQ Foods. So no streaming possible with react-query atm
@jherr
@jherr 11 ай бұрын
Yeah, I have to take another look at that. There might be a way to get that to work.
@josevsebastian2909
@josevsebastian2909 11 ай бұрын
can't you just use the loading and error components in the app router instead?? (error.tsx and loading.tsx in same level as the route.tsx which is currently server side rendering)
@jherr
@jherr 11 ай бұрын
This demonstrates using error boundaries and suspenses within the component to all for a finer level of loading and error indication granularity. If you don't need that then feel free to use error and loading.
@josevsebastian2909
@josevsebastian2909 11 ай бұрын
@@jherr one of the reasons I'm invested in next js is because I don't have to deal with all those states. But I get it 👍
@yuvarajyuvi9691
@yuvarajyuvi9691 11 ай бұрын
@@josevsebastian2909 you can only use them in pages right , it will wrap up the whole page with the error fallback if something fails , but if u r only using that api response in particular component then I think its best to just wrap that piece of code with error boundary
@josevsebastian2909
@josevsebastian2909 11 ай бұрын
@@yuvarajyuvi9691 in page router it works like that. Wrapping everything in error boundary and suspense. But one of the main advantages of app router is that, it respects the nested layouts of different routes, only the "page" leaf component is replaced by the boundary, all the "layout" is still rendered. And most often that's the result you want, except when you need granular loading or error handling inside the "page" and even that can be done using parallel routes.
@griffadev
@griffadev 11 ай бұрын
Neat trick but I wonder the benefits, we get no server rendering of data, so doesn't work for no js/crawlers and more complex than an API and use query
@jherr
@jherr 11 ай бұрын
What do you mean get no server rendering of data? I showed every time that the data is server rendered. Have a look at the HTML I showed from the page source. That's coming back from the server.
@griffadev
@griffadev 11 ай бұрын
@@jherr I guess not if you disable js, as the promise has the data, and not the html? I guess I can see the value if the API is slow enough you wouldn't want it on initial render maybe
@jherr
@jherr 11 ай бұрын
@@griffadev If you look at the output of the server, regardless of whether the client has disabled JavaScript or not, the HTML coming out of the server has all the rendered HTML including the HTML that comes in late off the suspense because the connection is held open until the suspense completes.
@griffadev
@griffadev 11 ай бұрын
@@jherr that makes sense, still getting used to this react sever component world, thanks for the explanation
@jherr
@jherr 11 ай бұрын
@@griffadev No problem. The whole client/server isomorphic thing has always been a little complicate when you look into the details. It was simpler with the Pages Router because the flow between getServerSideProps and then rendering, then sending it to the client, and hydrating and running there, blah blah blah, was pretty straightforward. Now, with RSCs and streaming, things are getting more complicated. They are CONSISTENT, which is cool, but complex.
@wsh4and
@wsh4and 11 ай бұрын
I dont think we need to give underscore for the component name, Nextjs is smart enough to not include it as part of the route page as long it doesnt have page file in it, cmiiw
@jherr
@jherr 11 ай бұрын
You don't need to, but this makes it really explicit.
@VATNIK-KILLER
@VATNIK-KILLER 11 ай бұрын
you're just solving the data inside the client component, context, no context, don't matter it's cached.
@jherr
@jherr 11 ай бұрын
What's cached again? The only cache that is involved here is the fetch cache which is being turned off in the code.
@MrPlaiedes
@MrPlaiedes 10 ай бұрын
Love your videos but promises as props just feels wrong.
@MrZiyak99
@MrZiyak99 11 ай бұрын
i dont understand why u are using suspense and error boundaries when next has loading.tsx and error.tsx specifically for this. also why use react query here it does not make sense to me. lastly i feel like this is not a common usecase does the next/react team encourage us to do something like this? i feel like situations like this huge payload are fairly uncommon. plz do lemme know cause there is still a lot i need to learn about the app router
@jherr
@jherr 11 ай бұрын
error and loading are route level, using suspense gives you the ability to give skeletons/spinners at a component level. the addition of react-query is the basis of a nice pattern of re-fetch and valuable to folks who already use that library (or SWR) to manage promises. it's just further validation that the promises are indeed promises. as for common use case, there are lots of use cases. i've personally worked with very large data sets on both client and server side. the nice thing about the App Router is that we are no longer sending everything out of getServerSideProps to the client, now with RSCs some data can remain strictly on the server and we can limit the amount of data going to the client.
@MrZiyak99
@MrZiyak99 11 ай бұрын
@@jherr would parallel routes be a better alternative to the suspense and error boundry situation? also what do you think of the long term use of something like react query and swr with next now pushing to have mutations with fetch? i had never faced this data issue so far (mostly cause we still use pages at work haha) but will keep this in mind!
@jherr
@jherr 11 ай бұрын
@@MrZiyak99 parallel routes are for rendering separate sections of the page like the header/footer/nav or the cart, or something like that. And the idea is that each parallel route has its own sync/async behavior. I wouldn't create a parallel route for every component on the page. If you are going to do mutations via API route, then RQ or SWR would be fine. If you are going to do them via server action then RQ or SWR would not be necessary and you could use something like `useTransition` to monitor them.
@MrZiyak99
@MrZiyak99 11 ай бұрын
@@jherr i use parallel routes for every independent section on my page with their own error.tsx and loading.tsx files 😭 will keep whatever you've said in mind incase work does decide to move from pages to app great content thanks for the help!
@jherr
@jherr 11 ай бұрын
@@MrZiyak99 We are probably talking about the same thing. There is the main content section of the page and then other sections that can be independently rendered/streamed and that's what parallel routes are great for.
@wasd3108
@wasd3108 10 ай бұрын
but if you just await the result and put it into provider you get the exact same result... lol? promises were irrelevant
@rico5146
@rico5146 9 ай бұрын
I think so
@Chloe-Software-Dev
@Chloe-Software-Dev 11 ай бұрын
Why not just pass the actual data to context and then distribute it this seems just overcomplicaton
@jherr
@jherr 11 ай бұрын
That's another option, but in this case that would mean blocking the RSC making the request because it would have to await the request. That would mean that any other components it contained that had additional requests would be blocked on the request.
3 React Mistakes, 1 App Killer
14:00
Jack Herrington
Рет қаралды 113 М.
Server Actions: NextJS 13.4's Best New Feature
21:41
Jack Herrington
Рет қаралды 59 М.
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 4,8 МЛН
Super gymnastics 😍🫣
00:15
Lexa_Merin
Рет қаралды 107 МЛН
Wait for the last one! 👀
00:28
Josh Horton
Рет қаралды 107 МЛН
Next 14 Reveals React's Taint, Solves RSC Safety Issues
15:22
Theo - t3․gg
Рет қаралды 56 М.
10 common mistakes with the Next.js App Router
20:37
Vercel
Рет қаралды 186 М.
Next.js App Router REVIEW (Six Months In Prod)
16:10
Theo - t3․gg
Рет қаралды 59 М.
Legend: The Ultimate React State Manager?
12:28
Jack Herrington
Рет қаралды 44 М.
Are You In A React Cult?
13:27
Jack Herrington
Рет қаралды 29 М.
What are my thoughts on next.js app router vs pages router
11:04
Web Dev Cody
Рет қаралды 22 М.
Why I'm Finally Using This Next.js Tool Again
6:36
Josh tried coding
Рет қаралды 25 М.
Finally Fix Your Issues With JS/React Memory Management 😤
20:13
Jack Herrington
Рет қаралды 81 М.
Convex: The Next Level Firebase for Modern Developers
25:16
Jack Herrington
Рет қаралды 23 М.
Will the battery emit smoke if it rotates rapidly?
0:11
Meaningful Cartoons 183
Рет қаралды 31 МЛН
APPLE совершила РЕВОЛЮЦИЮ!
0:39
ÉЖИ АКСЁНОВ
Рет қаралды 4,1 МЛН
Cadiz smart lock official account unlocks the aesthetics of returning home
0:30