Replies: 2 comments
-
I checked your sandbox and think that's expected. Because when the data of the first That's an optimization for most infinite scrolling scenarios like FB or Twitter. If the first page doesn't change, we don't revalidate the second one. To make that dependent work, you can simply return the dependency as a part of the key (make it an array): return [`https://api.github.com/repos/${repo}/issues?per_page=${PAGE_SIZE}&page=${
index + 1
}&first_issue_for_current_page=${data[index][0]}`, data.length] But you don't need to use it, just take the first argument for fetcher: url => fetcher(url) Here's an example, hope that helps: https://codesandbox.io/s/swr-infinite-forked-c7uut |
Beta Was this translation helpful? Give feedback.
-
@shuding thanks so much for your reply. You're right, adding the By the way, in case it's useful, here's a fork with a slightly more realistic use case, where in the second |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Similar to Dependent fetching with
useSWR
, can you have auseSWRInfinite
which depends on the data returned from anotheruseSWRInfinite
?It doesn't seem to work for me - in the
getKey
function for the seconduseSWRInfinite
, if I try and access the data from the firstuseSWRInfinite
, it always seems to be out-of-date.I forked the CodeSandbox for the
useSWRInfinite
example and added a simple seconduseSWRInfinite
call which uses the data from the first, with aconsole.log
statement in thegetKey
function to see what it's getting. If you open the console, and click the "Load more" button to load page 2 of results, in this case we'd expect thegetKey
function to be called withindex=1
and we should have access to the second page of data, so we should seedata.length=2
, but unfortunately that never happens.The workaround I've found is to use a ref (
useRef
) to store the data first, and then ingetKey
if I useref.current
to access the data, it works - I get the latest data (it gets called withindex=1
anddata.length=2
), so it seems like a closure problem, but then I wonder why that doesn't affect theuseSWR
example, linked above? And I wonder if this is a safe workaround, or if this feature is just not supported and should be avoided.Thanks so much for any thoughts.
Beta Was this translation helpful? Give feedback.
All reactions