Best approach to using React Query to handle deeply nested data & updates? #6150
Unanswered
austinrbrown01
asked this question in
Q&A
Replies: 2 comments
-
Real question for real but no real answers... |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey All!
I'm starting a new project and React Query looks like an awesome library to use for async state management. I ran into an "issue" though regarding how to best handle some deeply nested data with regard to initial fetching and updates, and would love some input from the community on this. My data model looks something like this:
This is a fictitious example, but I think it gets to the crux of my question. In my application, users should be able to plan new
LongTripPlan
s usingTripSections
from previous trips that they've planned. Moreover, users can create newLongTripPlan
s and useActivities
from other trips, without copying over the entireTripSection
s.Initially, I was thinking that I could provide a custom
useQuery()
hook for each data type, and their data would get loaded in as they rendered. However, If a LongTripPlan is composed of 40 days, each day has 5 activities, I don't want users to have to make 200 queries to get the data for a single trip -- I don't think this is the right way to do this, but correct me if I'm wrong.My next thought was to ditch
id
fields on the objects belowLongTripPlan
and just load that data in statically, but that would reduce the users' ability to copyTripSections
andActivities
to new plans, since all children to a LongTripPlan would be tightly coupled to the Trip. Moreover, I'd have to send an update for an entire plan every time I modified a detail on an Activity.Maybe I could design an endpoint
/getLongTripData
that would provide all of the data at once, and use this to load the initial data into each query hook? I could also create separate endpoints for updating Days, Activities, and TripSections, so that this state could be updated independent of the other components. Is this the best practice?Would a library like immer be suited for this?
Looking forward to your thoughts!
Beta Was this translation helpful? Give feedback.
All reactions