Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger not re-rendering because of dedupingInterval #259

Closed
vjpr opened this issue Feb 13, 2020 · 2 comments
Closed

Trigger not re-rendering because of dedupingInterval #259

vjpr opened this issue Feb 13, 2020 · 2 comments

Comments

@vjpr
Copy link

vjpr commented Feb 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.

await updateComment(postId, commentId, newComment)
trigger('/post/' + postId)

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.

const newPost = {...post}
post.comments[0].liked = true
mutate(postUrl, newPost, false)
// no re-render

const newPost = {...post}
post.name = 'foo'
mutate(postUrl, newPost, false)
// re-render

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.

@vjpr vjpr changed the title Trigger not re-rendering Trigger not re-rendering - possible dedupingInterval issue Feb 13, 2020
@vjpr vjpr changed the title Trigger not re-rendering - possible dedupingInterval issue Trigger not re-rendering because of dedupingInterval Feb 13, 2020
@mxschmitt
Copy link
Contributor

Looks like a duplicate of #271

@shuding
Copy link
Member

shuding commented Feb 24, 2020

Fixed in #279. The patch will be included in the next release!

@shuding shuding closed this as completed Feb 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants