Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Minified React error #310: Rendered more hooks than during the previous render. #78396

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

Open
moroshko opened this issue Apr 22, 2025 · 6 comments
Labels
Redirects Related to redirecting. Server Actions Related to Server Actions.

Comments

@moroshko
Copy link

Link to the code that reproduces this issue

https://github.com/moroshko/rendered-more-hooks-issue

To Reproduce

Once the application is started in Dev mode, and http://localhost:3000 is visited, the following error appears:

Application error: a client-side exception has occurred while loading ...

and the console shows:

Rendered more hooks than during the previous render.

Then, the error magically disappears and the page is rendered as expected.

Visit here to experience this yourself.

Current vs. Expected behavior

There should be no error.

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 24.4.0: Wed Mar 19 21:16:31 PDT 2025; root:xnu-11417.101.15~1/RELEASE_X86_64
  Available memory (MB): 16384
  Available CPU cores: 12
Binaries:
  Node: 22.11.0
  npm: 10.8.1
  Yarn: 1.22.22
  pnpm: 9.14.2
Relevant Packages:
  next: 15.4.0-canary.2 // Latest available version is detected (15.4.0-canary.2).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Redirects, Server Actions

Which stage(s) are affected? (Select all that apply)

Vercel (Deployed)

Additional context

If I do any of the following, the issue is gone:

  • Remove redirect("/dashboard") on the Home page and render some HTML instead.
  • Replace <Suspense> with a <div> in SignedInLayout.
  • Don't call the getAuth() server action in Providers.
@github-actions github-actions bot added Redirects Related to redirecting. Server Actions Related to Server Actions. labels Apr 22, 2025
@glekner
Copy link

glekner commented Apr 22, 2025

Also getting this with PPR enabled when reaching a 404 page.
This is Sentry's AI explaining what happened.
Any thoughts? @timneutkens

Root Cause of the Issue

Router component's conditional logic, triggered by an unknown page, alters hook execution, violating React's Rules of Hooks during re-render.

User navigates to unknown page, initiating a page load and triggering the Next.js routing system, which is the entry point for rendering the requested documentation page.

The user's navigation action triggers Next.js's page rendering process, starting with the Router component.

The Router component in src/client/components/app-router.tsx begins its rendering process, initializing its state and preparing to render the appropriate content based on the current route.

// src/client/components/app-router.tsx
function Router(props) {
  // ...
}

The Router component is responsible for handling navigation and rendering the correct content. Its internal logic determines which components to render and how to manage the application's state.
(See src/client/components/app-router.tsx)

Conditional logic within the Router component is triggered specifically by the unknown route, leading to a unique execution path during the rendering process.

// src/client/components/app-router.tsx
function Router(props) {
  // ...
  if (/* some condition based on props.canonicalUrl */) {
    // Execute hooks A, B, C
  } else {
    // Execute hooks A, B, D, E
  }
  // ...
}

This conditional logic, based on the route or other factors, determines which hooks are executed. The /docs/migrate-new-tenandt route triggers a specific branch of this logic.
(See src/client/components/app-router.tsx)

The useMemo hook on line 211 in src/client/components/app-router.tsx is reached after a different number of hooks have been executed compared to the previous render, violating React's Rules of Hooks.

// src/client/components/app-router.tsx
const { searchParams, pathname } = useMemo(() => { ... }, [canonicalUrl]);

Because the conditional logic altered the hook execution path, the useMemo hook is now being called in a different order or after a different number of hooks than before.
(See src/client/components/app-router.tsx)

React detects the hook mismatch and throws an error: "Rendered more hooks than during the previous render.", halting the rendering process.

The error message indicates that React's internal hook tracking mechanism has detected an inconsistency in the number and order of hooks being called during the rendering process.

@wlcharlie
Copy link

Also encounter this issue in next 15.1.3

@himself65
Copy link
Contributor

I can see this error on next.js 15.3.1

@himself65
Copy link
Contributor

Image

@glekner
Copy link

glekner commented Apr 30, 2025

@wlcharlie @himself65 @moroshko
For me, it was caused by a Clientside component mounted in my layout.tsx trying to run a server action.
Since my app is in a 404 page, the server action will always fail, combine that with the 404 trying to render, I almost always got this error

@aaronmhargrove
Copy link

aaronmhargrove commented May 2, 2025

Running into this issue as well on 15.1.2, similar cause to what @glekner discovered. One of our client context providers in our root layout file fetches some data and then updates its internal value with whatever it got from the server. This fetch is done via a server action which does no mutations, just fetches some data.

Though, while the layout client component is fetching its data a redirect happens, and causes this error.

  GET /one 200 in 78ms
~Root layout client component calls action to fetch data
~Page level (page one) server component performs redirect based on some condition
  GET /two 200 in 61ms
~Server action starting on server (sent from root layout)
~Resolving data from server action
  POST /one 200 in 92ms
  GET /two 200 in 37ms
~Server action starting on server
~Resolving from server action
  POST /two 200 in 72ms

Next Info:

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:40 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6031
  Available memory (MB): 36864
  Available CPU cores: 14
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: 1.22.22
  pnpm: 9.15.5
Relevant Packages:
  next: 15.2.1 // There is a newer version (15.3.1) available, upgrade recommended! 
  eslint-config-next: 15.2.1
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.4.5
Next.js Config:
  output: N/A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Redirects Related to redirecting. Server Actions Related to Server Actions.
Projects
None yet
Development

No branches or pull requests

5 participants