add useCallback in useSWRInfinite example when need to use setSize
See original GitHub issueBug report
Description / Observed Behavior
What kind of issues did you encounter with SWR?
setSize
infinite loop in useSWRInfinite
when combine with intersection-observer
because we need to use useEffect
to listen scroll,
so if use setSize()
will be infinite loop
Expected Behavior
How did you expect SWR to behave here?
maybe can add useCallback
is fine
like
const loadMore = useCallback(() => setSize((state) => state + 1), [setSize]);
Repro Steps / Code Example
Or share your code snippet or a CodeSandbox link is also appreciated!
Additional Context
SWR version. Add any other context about the problem here.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Why useSWRInfinite call my backend twice for every request?
const { data, size, setSize } = useSWRInfinite( (index) ... specify in your getKey function that you just want to make one request...
Read more >How and when to use React useCallback() - Amber Wilson
In short, React's useCallback hook is used to wrap functions. ... Most blog posts I have read on useCallback contain example code where...
Read more >Integration with Directual | Quarkly docs
For our application to work, we need to create two Data structures: ... const { data, error, size, setSize, mutate } = useSWRInfinite(....
Read more >react has detected a change in the order of hooks - You.com
const { data, error, size, setSize } = useSWRInfinite( (pageIndex, ... Hooks always need to be called in the same order (so adding...
Read more >Next.js and MongoDB full-fledged app Part 4 - Hoang Vo
For this API, we need to use the database and auths middlewarefor database connection and ... import { useCallback, useRef } from "react";....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@koba04 Sorry I should explain a bit more. I think he’s referring to this example: https://swr.vercel.app/examples/infinite-loading, which is very basic. Added the label since we need an example specifically for infinite scroll cases (load more when you hit the page bottom).
Just use an extra “should” state with an useEffect