TanStackT
TanStack13mo ago
5 replies
near-sapphire

Getting a single item from query cache

Currently I have an api for getting all /folders and all /files (using React + Vite in client). In the client, the sidebar component fetches the folders which in turn makes them into links (example returned folder{name: "Primary", id: 'randomIDaeqw123'}). Main component is essentially a route that fetches the files of the current folder when you navigate using a folder link.

Now, I want to create a Breadcrumb component inside the Main. I already have a query to get all folders which also coincides with the data needed in this Breadcrumb component.

1. I'm thinking of just filtering/finding a single folder from the cached folders. Is it expensive to filter the cached queryData just to get a single item?

const current = queryClient
    .getQueryData<Folder[]>(['folders'])
    ?.find((f) => f.id === (params.folderId as string));


2. Should I just create a getSingleFolder endpoint + query hook?

Appreciate any tip!
folderAndFiles.png
Was this page helpful?