Skip to content

Noticed useSWR rendering my component twice - even when not calling the fetcher #4210

@nsunga

Description

@nsunga

Bug report

Description / Observed Behavior

What kind of issues did you encounter with SWR?

  • Using SWR in an isomorphic application (Next.js + React)
  • Regardless of conditionally calling the fetcher within useSWR, Im noticing my component render twice - even with reactstrictmode disabled

Expected Behavior

How did you expect SWR to behave here?

  1. I am simulating a fetch call on server side and passing this data down to an Age component
  2. Inside the Age component, useSWR will conditionally call the fetcher if the server did not pass in data
  3. As expected, the fetch call is not called on the client side - because the server data exists
  4. However, Im noticing that my component is rendering twice ie. notice the chrome dev tools logs happen twice in the sandbox - ive disabled react strict mode to remove re-render noise too
  5. All of the data returned from useSWR is still the same - so Im puzzled by the re-render
  6. When I do fetch calls using useEffect instead, my component only renders once

Repro Steps / Code Example

Or share your code snippet or a CodeSandbox link is also appreciated!

Relevant files:

  • app/page.tsx is the bulk of the page
  • app/components/age-v2.tsx is the component that uses useSWR
  • app/utils/fetch-age.ts is a dummy promise to simulate a fetch request with wait times
  • next.config.ts Ive disabled reactstrictmode here to remove the bloated re-renders for debugging purposes

Additional Context

SWR version.
Add any other context about the problem here.

  "dependencies": {
    "next": "15.5.7",
    "react": "19.0.0",
    "react-dom": "19.0.0",
    "swr": "2.3.8"
  }
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions