Repository navigation
How to use useMany hook with ids that may be duplicates? #6411
Replies: 2 comments
|
Hey @SadmanYasar can you explain a bit more? getMany filters by given ids, having duplicate ids could be a problem. |
|
If the duplicates represent repeated references in your UI, fetch the records once and then reconstruct the display sequence from the original IDs.
For example, after fetching the unique IDs: const ids = [52, 52, 52];
const uniqueIds = [...new Set(ids)]; // pass this to useMany
// `records` is the array returned by the provider.
const byId = new Map(records.map(record => [String(record.id), record]));
const occurrences = ids.map((id, index) => ({
occurrenceKey: `${id}:${index}`,
record: byId.get(String(id)),
}));That produces three occurrences in the requested order without three identical requests. Keep missing records explicit (for example, show an unavailable placeholder) rather than silently changing the positions. Use an occurrence key when rendering; The result property is version-dependent: in Refine v4 it's typically |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Currently, if I have an array of ids e.g [52,52,52], the useMany hook only includes one object matching the id instead of three objects for three ids in the array. I am using Directus data provider. Is this related to Refine or Directus SDK? The code is pretty much the same as the docs
All reactions