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

Support conditional fetching in useSWRInfinite #936

Closed
em opened this issue Jan 28, 2021 · 4 comments
Closed

Support conditional fetching in useSWRInfinite #936

em opened this issue Jan 28, 2021 · 4 comments

Comments

@em
Copy link

em commented Jan 28, 2021

I've tried passing null instead of a getKey function to useSWRInfinite. Also tried returning null from getKey.

In both cases, this results in a deadlock where no additional pages will be loaded when setSize is used to increase the size.

@wildfrontend
Copy link

@em maybe you can try
const loadMore = useCallback(() => setSize(state=> state +1),[setSize])

but another case is when fetch request error, useSWRInfinite will also return infinite loop

@em
Copy link
Author

em commented Feb 4, 2021

Thanks wildfrontend. I was able to workaround before I filed the issue by just restructuring things with wrapper components. Conditional fetching in useSWR is really just sugar anyway and the same thing can be accomplished by conditional rendering, it can just make your component IA not very nice. I filed the issue because it's a nice-to-have feature request for useSWRInfinite to have parity with useSWR.

As an aside - there are a lot of caveats with useSWRInfinite, some are addressed in the original PR, but not documented in the official docs. Would be really nice for those to be big disclaimers at the top.

@shuding
Copy link
Member

shuding commented Apr 4, 2021

FYI you can pass null as the first argument to useSWRInfinite and it will pause data fetching. Can you provide a reproduction?

@shuding shuding closed this as completed Apr 4, 2021
@shuding shuding mentioned this issue Apr 4, 2021
@dev-smart-ui
Copy link

dev-smart-ui commented Jul 6, 2023

Hi I have same problem , if chat id undefined on first render , useSWRInfinite send requests on second render receive data from API , but const {data }=undefined even if i see data in network tab google chrome

  const { data, error, isLoading, isValidating, mutate:mutateCurrentChat, size, setSize } =
    useSWRInfinite<IConversation>(
      (pageIndex, previousPageData) => { 
        if (previousPageData && !previousPageData.length) return null; // reached the end
        if (!chatId) return null;
        return `${API.singleChat(chatId)}?page=${pageIndex + 1}&page_size=20`;
      },
      fetcher,
      {
        focusThrottleInterval: 8000,
        refreshInterval: 150000,
      }
    );

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

4 participants