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 assumed trigger would force useSWR('/post/' + id) to re-render in the parent component but this is not the case. mutate does not help either.
What does trigger actually do? Does it talk to all useSWR hooks in the app and set state so that they re-render?
And what is the best approach for working with nested entities, where the first request returns an entity and its nested entities. But then you want to modify a nested entity. Can I avoid N+1 fetching every time I change a nested entity property? Could I add the Comment entities into SWR's cache from the Post request and then render each nested entity using useSWR instead of passing the nested data?
Maybe initialData could help? Would useSWR(`posts/${id}/comments/${commentId}`, {initialData: comments[0]}) be heading on the right path?
Thanks!
EDIT
So it appears that it is re-rendering if I wait 2 seconds before sending the PUT request. This seems to correspond to the default dedupingInterval: 2000. Could this be some kind of bug?
The issue now though is that when running mutate on slow 3G, the page is not updating immediately. Should it?
I am doing:
mutate
await request(...)
trigger
EDIT 2
So it seems that mutate will automatically cause useSWR with same key to re-render. My issue seems to be that I was doing a shallow copy of my Post, and SWR was not seeing it as modified.
How does SWR decide when to re-render? I assume it is just using setState() internally? If this is the case maybe it should be documented that this is what is happening.
The text was updated successfully, but these errors were encountered:
vjpr
changed the title
Trigger not re-rendering
Trigger not re-rendering - possible dedupingInterval issue
Feb 13, 2020
vjpr
changed the title
Trigger not re-rendering - possible dedupingInterval issue
Trigger not re-rendering because of dedupingIntervalFeb 13, 2020
I have a
useSWR('/post/' + id)
which returns a Post model with nested Comments{id, title, comments}
. I use this to render the post with its comments.Then in another component I want to modify comments.
I assumed
trigger
would forceuseSWR('/post/' + id)
to re-render in the parent component but this is not the case.mutate
does not help either.What does
trigger
actually do? Does it talk to alluseSWR
hooks in the app and set state so that they re-render?And what is the best approach for working with nested entities, where the first request returns an entity and its nested entities. But then you want to modify a nested entity. Can I avoid N+1 fetching every time I change a nested entity property? Could I add the Comment entities into SWR's cache from the Post request and then render each nested entity using useSWR instead of passing the nested data?
Maybe
initialData
could help? WoulduseSWR(`posts/${id}/comments/${commentId}`, {initialData: comments[0]})
be heading on the right path?Thanks!
EDIT
So it appears that it is re-rendering if I wait 2 seconds before sending the PUT request. This seems to correspond to the default
dedupingInterval: 2000
. Could this be some kind of bug?The issue now though is that when running
mutate
on slow 3G, the page is not updating immediately. Should it?I am doing:
EDIT 2
So it seems that mutate will automatically cause
useSWR
with same key to re-render. My issue seems to be that I was doing a shallow copy of my Post, and SWR was not seeing it as modified.How does SWR decide when to re-render? I assume it is just using
setState()
internally? If this is the case maybe it should be documented that this is what is happening.The text was updated successfully, but these errors were encountered: