-
I have a use case where I can definitely work with the current library –which is awesome! thanks 😄–, but I want to propose something that would make me feel more confident about the code I'm writing. Maybe I'm missing some part of the library which would allow me to do what I want. Example use caseI'm getting a bunch of records for different users with the following code: const results = useQueries({
queries: userIds.map(userId => ({
queryKey: ['records', ':userId', userId],
queryFn: () => getRecordsForUser(userId),
})),
}) Here, Then I want to process the results to create a dictionary with the key being the What I'm doing right now is the following: const records = {}
userIds.forEach((userId, idx) => {
records[userId] = results[idx].data
}) That works just fine, but I'm implicitly relying in the order of In my particular use case, the records have a reference to the user id, so I could write a test that checks the records stored in the dictionary are actually records of the user represented by the key they're being stored in. ProposalI was hoping I could access the const results = useQueries({
queries: userIds.map(userId => ({
queryKey: ['records', ':userId', userId],
queryFn: () => getRecordsForUser(userId),
meta: { userId } // <-- new
})),
})
const records = {}
results.forEach(result => {
records[result.meta.userId] = results.data
}) Note how I'm now iterating on the results instead of the I believe this proposal would allow for writing cleaner and easier to maintain code. I'd also be happy to make the changes if this proposal makes sense. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
you can easily attach the |
Beta Was this translation helpful? Give feedback.
-
@TkDodo what if I'd want to store the entire result of a single query in the map under the user id? Is there any way I could do that because data might not be there if there is error for example? E.g. in the combine method I'd like to return |
Beta Was this translation helpful? Give feedback.
you can easily attach the
userId
to the result object in thequeryFn
, though I would expect thegetRecordsForUser(userId)
function to potentially contain theuserId
already. Then, you can usegroupBy
or something similar to group the results by that id. So generally, if you want something available on the result, add it to thedata
field, which is what thequeryFn
returns.