How to load a list of objects and then access individual elements of that list deep down a tree
Unanswered
Chris posted this in #help-forum
ChrisOP
Hi! I've got a list of posts which I want to display as a list. The user can then click on a given post, and a bunch very granular, editable, information will be displayed (so post information will be required quite deep down the tree)
If I load the list up front, caching seems like it would only prevent me from needing to refetch the entire list, so when I'm in an individual element my options are
- Prop drilling
- Run a separate query on a per-post basis.
- Context
None of these seem like good options because
- Prop drilling is fine, but I have a few different data types like this on the same page, so will be difficult to manage
- The per post query feels very inefficient
- Context will lead to unnecessary rerenders
The only thing I can think of is creating a pattern in my server where I have a base "get all" function, and then build on top of that, where the "get all" function is recalled in every other function - something liket he below
If I load the list up front, caching seems like it would only prevent me from needing to refetch the entire list, so when I'm in an individual element my options are
- Prop drilling
- Run a separate query on a per-post basis.
- Context
None of these seem like good options because
- Prop drilling is fine, but I have a few different data types like this on the same page, so will be difficult to manage
- The per post query feels very inefficient
- Context will lead to unnecessary rerenders
The only thing I can think of is creating a pattern in my server where I have a base "get all" function, and then build on top of that, where the "get all" function is recalled in every other function - something liket he below
export const getCachedPosts = unstable_cache(
async (): Promise<post[]> => {
const postPromise = prisma.post.findMany()
const data = await Promise.all([
postPromise,
]);
const posts = data[0];
return posts;
},
['posts']
);
export async function getCachedPost(id: string) {
const posts = await getCachedPosts();
const post = posts.find((post) => post.id === id);
}12 Replies
If I load the list up front, caching seems like it would only prevent me from needing to refetch the entire listContext of course :D why force n+1 if you can just do 1 query.
how do you want your application to behave?
what would happen if i click on a post. Does the client takes care of loading the information or is the user redireted to
what would happen if i click on a post. Does the client takes care of loading the information or is the user redireted to
/post/1 ?ChrisOP
Thanks for responding! Figured it'd be easier to just share a video - this is the project I'm trying to move from CRA to Next.
I have a list of emails on the left. When you click on an email, the "tasks" pane opens, showing a list of tasks. You can then click into each task to display detailed information associated with that task. In this example there's an invoice (a summary in purple, which the user can then click into to edit).
Not too fused about routing vs client rendering for the navigation, as long as it's responsive. I've got it set up at the at the moment so each pane is a new route, but I guess I might need to do some pre-rendering for speed optimisation, which might be unnecessary?
I've been wary of context because I feel I'm going to end up with lots of re-renders when the a user edits an invoice or a task.
I have a list of emails on the left. When you click on an email, the "tasks" pane opens, showing a list of tasks. You can then click into each task to display detailed information associated with that task. In this example there's an invoice (a summary in purple, which the user can then click into to edit).
Not too fused about routing vs client rendering for the navigation, as long as it's responsive. I've got it set up at the at the moment so each pane is a new route, but I guess I might need to do some pre-rendering for speed optimisation, which might be unnecessary?
I've been wary of context because I feel I'm going to end up with lots of re-renders when the a user edits an invoice or a task.
ChrisOP
Any thoughts @aardani ? 🙇â€â™‚ï¸
I've got it working with Redux for now, so no worries if not.
I've got it working with Redux for now, so no worries if not.
ChrisOP
At the moment the URL doesn't change at all
This is the route structure
And then this is my entity layout.tsx, which is a Server component, where I load all the data
And everything is contained within the Workspace component, where I load all the data into the Redux store
Open to changing this though, just looking to learn what the "best" approach might be
ChrisOP
I think I'm coming to the conclusion that the whole "load all the data up front" just into currently something Next wants you to do
Hopefully they'll make the cache more flexible in the future, to allow this