Managing Multiple Promises in Next.js

I'm working on a project where we display multiple reports on a single page. Each report has its own set of filters, and currently, I'm fetching data from the database separately for each component. However, when I change the filter in one component, the entire page re-renders, causing all components to refetch data. Since I'm fetching data on the server side, I can't use memoization to prevent rendering. How do you typically handle this situation to avoid unnecessary page renders when only one component's filter changes?
Solution:
These are trade-offs. Server components are really great to have access to all your data without creating complex APIs with potentially huge JSON payloads. But if you want fine-grained control of how the UI updates then you're going back to what hooks, APIs, and state management are good for. And that's perfectly fine, albeit it might be more complexity than you hoped for. Load the initial page from the server. Then handle updates on the browser....
Jump to solution
20 Replies
Hamza K Dev
Hamza K DevOP7mo ago
So this is the page where as you can see, Graph has a range filter, now when I change this filter, other reports are also fetched.
No description
TerjaN
TerjaN7mo ago
you are using query params as the filter state ?
Hamza K Dev
Hamza K DevOP7mo ago
Yes.
TerjaN
TerjaN7mo ago
maybe caching with fetch('...', cache) ?
Hamza K Dev
Hamza K DevOP7mo ago
I am not using Fetch. Directly calling the prisma db
TerjaN
TerjaN7mo ago
I dont have too much experience with cache of react but its seems like the case to use it https://react.dev/reference/react/cache
cache – React
The library for web and native user interfaces
Hamza K Dev
Hamza K DevOP7mo ago
There should be a better way to acheive this. Suspense should be smart enough to realize that the key hasn't changed so it shouldn't refetch, like we have usememo.
TerjaN
TerjaN7mo ago
yeah, I would try some manual tests just in case
Hamza K Dev
Hamza K DevOP7mo ago
I have tried React cache as well doesn't seem to be caching anything LOL
TerjaN
TerjaN7mo ago
xD
TerjaN
TerjaN7mo ago
cache – React
The library for web and native user interfaces
TerjaN
TerjaN7mo ago
if you can send some code it would be great maybe I catch something
Hamza K Dev
Hamza K DevOP7mo ago
So here as you can see Graph and Reports and in their own suspense boundary, on initial reload, it works as expected. but after applying filters, there is no loading state, because both components are fetched again.
TerjaN
TerjaN7mo ago
I'm not sure if having a key in Suspense prevents refetching, if you pass as key a constant, it wouldnt refetch?
Hamza K Dev
Hamza K DevOP7mo ago
I don't see any freaking point of key prop in Suspense.
Sturlen
Sturlen7mo ago
I'm not sure if what you're trying to do is supported in next with server components and suspense. the closest I know of is partial rendering, but that doesn't seem to fit your use case. One solution is making each report a client component and using Tanstack Query to fetch the new data for it when it's filters change.
Routing: Linking and Navigating | Next.js
Learn how navigation works in Next.js, and how to use the Link Component and useRouter hook.
Solution
michaeldrotar
michaeldrotar7mo ago
These are trade-offs. Server components are really great to have access to all your data without creating complex APIs with potentially huge JSON payloads. But if you want fine-grained control of how the UI updates then you're going back to what hooks, APIs, and state management are good for. And that's perfectly fine, albeit it might be more complexity than you hoped for. Load the initial page from the server. Then handle updates on the browser.
Hamza K Dev
Hamza K DevOP7mo ago
@michaeldrotar , Agreed, I have implemented TRPC for views that have many independent components.
Want results from more Discord servers?
Add your server