You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure if this is a bug, or if this behavior is intended, but I was running into an issue with the line of code below.
So, I'm using useSWR(/api/users/${id}, fetch, {suspense: true}) where the key is dynamic based on the id of the object I want to get (in this contrived example the user's id).
My code looks something like this exampled:
function useUser(id) {
const user = useSWR(`/api/users/${id}`, fetch, {suspense: true})
console.log(user)
return user
}
When this hook is called multiple times with different IDs, the logs would look like this:
// render 1 with user id 1
{ id:1 }
// render 2 with user id 2
undefined
// render 3
{ id: 2 }
The reason behind this is the two LOC referenced below.
So, my question is, in render 2 on L342 typeof data = Object, not undefined because the data is reset on line 358 (afterwards). Should there be a line before line 342 that checks to see if the keyRef.current and the new key are then different and reset data there so that the revalidate can happen immediately?
I quickly implemented this locally and this fixed my issue and prevented render 2 happening where the data was undefined.
* fix suspense on hydration
* refactor the code; add error to the cache
* fix suspense and error catching
* add test case for #57
* Update src/use-swr.ts
* Apply suggestions from code review
Co-Authored-By: Paco <34928425+pacocoursey@users.noreply.github.com>
I'm not sure if this is a bug, or if this behavior is intended, but I was running into an issue with the line of code below.
So, I'm using
useSWR(
/api/users/${id}, fetch, {suspense: true})
where the key is dynamic based on the id of the object I want to get (in this contrived example the user's id).My code looks something like this exampled:
When this hook is called multiple times with different IDs, the logs would look like this:
The reason behind this is the two LOC referenced below.
So, my question is, in render 2 on L342
typeof data = Object
, not undefined because the data is reset on line 358 (afterwards). Should there be a line before line 342 that checks to see if thekeyRef.current
and the newkey
are then different and reset data there so that the revalidate can happen immediately?I quickly implemented this locally and this fixed my issue and prevented render 2 happening where the data was undefined.
Thanks! Loving this library so far.
https://github.com/zeit/swr/blob/9b51b7d066335d7249d114e2b9a2cb5d633795b6/src/use-swr.ts#L342
https://github.com/zeit/swr/blob/9b51b7d066335d7249d114e2b9a2cb5d633795b6/src/use-swr.ts#L358
The text was updated successfully, but these errors were encountered: