Replies: 1 comment
-
If the amount of endpoints is fixed I would recommend to run If the amount of endpoints is dynamic, you can pass the list to function multiFetcher(...urls) {
return Promise.all(urls.map(url => fetcher(url))
}
const { data: [data1, data2, data3] } = useSWR([url1, url2, url3], multiFetcher) Something like that 👆 Note this will create a cache key with the array of all of them and it will revalidate all of them at the same time. If you want more granular control you will need to use more than one useSWR call. |
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
-
I've used useSWR for single fetch components and I love it, but now I see myself in a pickle, I gotta represent a chart that pulls data from many different endpoints, and as I have it now, it works without SWR, I created a custom hook, do all request and with Promise.all I format the code into a JSON for the chart, something like this:
(a DataLoader is like: {
apiCall: fetch(urlX).then(r => r.data),
dataTypes: [objects to map results into chart values]
}
Any way to involve useSWR into that kind of mix where you need to do something when all requests finish? (as a plan B, I'll leave this as it is, but I'd like to use SWR for it's caching capabilities).
Beta Was this translation helpful? Give feedback.
All reactions