question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

revalidateOnMount is broken on useSWRInfinite

See original GitHub issue

Bug report

Description / Observed Behavior

useSWRInfinite is not revalidating on mount when prop is set in config

Expected Behavior

It should revalidate on mount

Repro Steps / Code Example

CodeSandbox

mounting/unmounting has no effect on the data reendered on the page. note how it works with a useEffect call to mutate()

Additional Context

SWR version. 0.4.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pedro-pedrosacommented, Jan 29, 2021

Sorry I probably should have explained my codesandbox a little bit better. each mount/unmount increments a counter by 1. this counter is passed as prop to a component that gets mounted/unmounted. this component calls useSWRInfinite and provides a fetcher function that depends on this counter… so in theory, each mount/unmount cycle should make the fetcher function print 2 more numbers than the previous cycle.

however the number never changes on each mount/unmount, it’s always 0 as it’s the first value returned by the fetcher. when it’s mounted, if you click “revalidate”, it calls mutate which will run the fetcher again and print the true values. so you just have to click mount/unmount a few times and then revalidate to see the issue.

if you enable the useEffect checkbox, you will see that a simple mutate() call on mount works the way that revalidateOnMount should.

0reactions
shudingcommented, Sep 5, 2021

Confirmed that this is fixed in the latest version: https://codesandbox.io/s/quizzical-agnesi-fzjkw?file=/src/Component.js. One thing to keep in mind is dedupingInterval can also make a difference here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API
SWR is a React Hooks library for data fetching. SWR first returns the data from cache (stale), then sends the fetch request (revalidate),...
Read more >
swr: Versions
In this release, useSWR , useSWRInfinite and useSWRImmutable will return an extra ... fix: useSWRInfinite revalidates with revalidateOnMount by @koba04 in ...
Read more >
Renovate Bot Package Diff
If it's not mounted yet and it should revalidate on mount, revalidate. ... + // We have to several type castings here because...
Read more >
reactjs - SWR not pulling from cache
Depending on your use cases, you probably want these options off too: revalidateOnMount; revalidateOnFocus; revalidateOnReconnect. As the other ...
Read more >
React Hooks library for remote data fetching
This PR adds a new useSWRInfinite API for infinite loading, ... fix: useSWRInfinite revalidates with revalidateOnMount by @koba04 in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found