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

React Hook Order Error with Multiple useSWR Hooks in Next.js with Suspense #2849

Open
LarsFlieger opened this issue Dec 4, 2023 · 3 comments
Labels
area: suspense bug Something isn't working

Comments

@LarsFlieger
Copy link

LarsFlieger commented Dec 4, 2023

Bug report

Description / Observed Behavior

In a Next.js project using SWR with Suspense, an issue arises when more than two useSWR hooks are used within a single component. The errors encountered are:

  • Warning: React has detected a change in the order of Hooks called by ComponentExample. This will lead to bugs and errors if not fixed.
  • Uncaught Error: Update hook called on initial render. This is likely a bug in React. Please file an issue.

The issue occurs in Component.tsx, where three fetch operations are initiated using useSWR with suspense: true. The intention is to perform three separate data fetches with delays and console log the outputs.

"use client";

import useSWR from "swr";

const fetchWithDelay = async (url: string) => {
  const delay = parseInt(url) * 1000;
  await new Promise((resolve) => setTimeout(() => resolve(url), delay));
  return url;
};

export const ComponentExample: React.FC = () => {
  const { data: dataAfter1 } = useSWR("1", fetchWithDelay, {
    suspense: true,
  });
  console.log(dataAfter1);

  const { data: dataAfter2 } = useSWR("2", fetchWithDelay, {
    suspense: true,
  });
  console.log(dataAfter2);

  const { data: dataAfter3 } = useSWR("3", fetchWithDelay, {
    suspense: true,
  });
  console.log(dataAfter3);

  return (
    <main>
      <p>Page loaded!</p>
    </main>
  );
};

Expected Behavior

The expectation is that using multiple useSWR hooks in a single component should work seamlessly, particularly when leveraging Suspense in React.

Repro Steps / Code Example

A minimal reproduction of this issue is available in this GitHub repository. The component tries to perform three separate data fetches with a delay function, using SWR with Suspense.

Additional Context

SWR version: ^2.2.4
React version: ^18
Next.js version: 14.0.3

I welcome any feedback or suggestions to fix this problem. If anyone has encountered similar issues or has insights into resolving this, please feel free to contribute. Collaborative efforts to debug and find a solution are highly appreciated.

@Branchverse
Copy link

Branchverse commented Dec 6, 2023

Same problem here! Appreciate a quick fix

@promer94 promer94 added the bug Something isn't working label Dec 19, 2023
@promer94
Copy link
Collaborator

duplicate of #2702

@FelixZY
Copy link

FelixZY commented Apr 3, 2024

For future readers: note that #2702 was closed in favor of this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: suspense bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants