Reputation: 2085
I want to use SWR to fetch data for the logged in user but I am not sure how to do this in the correct way.
e.g. say i want to get data from the api path api/get/cards
for the particular user who is logged in. I could use api/get/cards
for they key in SWR but then I assume the cache won't know that the data is specific to the current user only? And then the cache will just provide the data for the last user to user it and not necessarily the user who is currently calling it? What is the best way of doing this?
Upvotes: 2
Views: 2008
Reputation: 50338
You can pass an array as the key
parameter to useSWR
for this precise scenario.
const { data } = useSWR(['/api/get/cards', userId], (url, userId) => { /* Fetching logic */ })
The key of the request will now be the combination of both values, and will revalidate if the userId
changes.
Upvotes: 4