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 miss the revalidate function exported from useSWR pre 1.0
I could be exported as a "refresh" function from the hook.
With 1.0 the function has been deprecated and its suggested to use mutate directly without any arguments. The problem is that this can introduce hard to catch bugs in your code (especially when you used revalidate before).
Leaving the client code unchanged upon clicking the span we will then see "[object Object]" as the text.
That is because, just as before, we simply re-export the mutate function from swr. Except that mutate takes arguments now, which revalidate didn't. And so it gets handed in the onClick handlers argument(s) which is the ClickEvent. That causes mutate to use the first argument to update the cache with this first argument.
We could fix this by changing the client code to this:
<spanonClick={()=>refresh()}>{ip}</span>
However, internal implementation details should not concern the user of a hook.
The correct hook would have to wrap the mutate function into a callback.
I would like to understand if I got something wrong here?
What was the reason to remove the very convenient revalidate function in the first place?
We are using many swr hooks and all re-export a "refresh" function. We would have to change/check every one of them to no accidently export mutate in this faulty manor.
What is the recommend way to offer refresh to hook users when swr is involved?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I miss the
revalidate
function exported fromuseSWR
pre 1.0I could be exported as a "refresh" function from the hook.
With 1.0 the function has been deprecated and its suggested to use
mutate
directly without any arguments. The problem is that this can introduce hard to catch bugs in your code (especially when you used revalidate before).Lets consider this really useful hook:
and this usage
When you press the span it should refresh the (external) IP address.
Now lets change the hooks implementation to update to swr >= 1.0
Leaving the client code unchanged upon clicking the span we will then see "[object Object]" as the text.
That is because, just as before, we simply re-export the
mutate
function from swr. Except thatmutate
takes arguments now, whichrevalidate
didn't. And so it gets handed in theonClick
handlers argument(s) which is theClickEvent
. That causesmutate
to use the first argument to update the cache with this first argument.We could fix this by changing the client code to this:
However, internal implementation details should not concern the user of a hook.
The correct hook would have to wrap the mutate function into a callback.
I would like to understand if I got something wrong here?
What was the reason to remove the very convenient
revalidate
function in the first place?We are using many swr hooks and all re-export a "refresh" function. We would have to change/check every one of them to no accidently export
mutate
in this faulty manor.What is the recommend way to offer refresh to hook users when swr is involved?
Beta Was this translation helpful? Give feedback.
All reactions