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.

add useCallback in useSWRInfinite example when need to use setSize

See original GitHub issue

Bug 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:open
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
shudingcommented, Jan 29, 2021

@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).

0reactions
hazae41commented, Aug 22, 2021

Just use an extra “should” state with an useEffect

const [shouldLoadMore, setShouldLoadMore] = useState(false)

const observer = useMemo(() => {
  return new IntersectionObserver(([e]) => {
    if (e) setShouldLoadMore(e.isIntersecting)
  })
}, [])

useEffect(() => {
  if (shouldLoadMore)
    pages.setSize(size => size + 1)
}, [shouldLoadMore])
Read more comments on GitHub >

github_iconTop 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 >

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