Pattern for data fetching
Which pattern is better in the following case (react query plus next js)
A page where multiple child and grand child are dependent on some data from an api let's say user info api
Let say some component uses name, someuses contact and some use other things
Soo
1. I make the request at the top of the page
Store data in zustand (any state management library) and my components just use that or I pass down data, but here I make my page 'use client'
2. Each component makes same request with same key ('user-data')
6 Replies
harsh-harlequin•12mo ago
do you want to use nextjs with start?
rival-blackOP•12mo ago
Yes
I'm using the server pages aka react server component
Soo I'm curious which is better
harsh-harlequin•12mo ago
how does that mix with start?
I have no next experience so I need some more context
rival-blackOP•12mo ago
Basically in first you lose server components thing but make request once.
harsh-harlequin•12mo ago
what you describe can be solved using TanStack Router context quite easily. in one parent route's beforeLoad you perform that API call and return the value.. then the value is available to child routes via router context
rival-blackOP•12mo ago
I don't think we can use that I Next, I need to dig in