App Router Error: Rendered more hooks than during the previous render #59493
-
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
Hi, Are you calling const App = () => {
if (condition) router.replace()
return <div />
} That'll lead to disaster, because it is setting state as your render, which means the render is not pure, which means you are potentially batching more state updates, while trying to reflect an state update.
The root cause being that, Anyway, could you see if this is the issue? |
Beta Was this translation helpful? Give feedback.
-
I am wondering if this could be triggering for us from a React Server Component? We have an RSC as our main We are using Next 15.2.0 |
Beta Was this translation helpful? Give feedback.
-
Temporary fix is to use client side redirection instead. Example code: // instead of:
// redirect(profileUrl(session.user));
// https://github.com/vercel/next.js/issues/57455
return <RedirectionPage redirectUrl={profileUrl(session.user)} replace />; import SimpleContainer from '@/containers/components/SimpleContainer';
import { Card, CardBody, CardText, CardTitle } from 'reactstrap';
import React from 'react';
import { LoadingIcon } from '@/containers/components/LoadingIcon';
import RedirectionPageClient from '@/containers/RedirectionPageClient';
export default function RedirectionPage({ redirectUrl, replace }: { redirectUrl?: string; replace?: boolean }) {
return (
<>
{redirectUrl && <RedirectionPageClient redirectUrl={redirectUrl} replace={replace} />}
<SimpleContainer>
<Card className="p-4">
<CardBody className="text-center">
<CardText className={'mx-auto mb-3 opacity-50'}>
<LoadingIcon isLg />
</CardText>
<CardTitle tag="h2">Redirecting</CardTitle>
<CardText>Please wait a second</CardText>
</CardBody>
</Card>
</SimpleContainer>
</>
);
} export default function RedirectionPageClient({ redirectUrl, replace }: { redirectUrl: string; replace?: boolean }) {
const router = useRouter();
useEffect(() => {
if (replace) {
router.replace(redirectUrl);
} else {
router.push(redirectUrl);
}
}, [redirectUrl, replace, router]);
return null;
} |
Beta Was this translation helpful? Give feedback.
Hi,
Are you calling
router.replace
or any method under the router, while rendering? as in:That'll lead to disaster, because it is setting state as your render, which means the render is not pure, which means you are potentially batching more state updates, while trying to reflect an state update.
The root cause being that,
router
methods (replace, push, etc) are now using React's state management, useReducer. You can install redux-devTools and see the actions being dispatched! (Because the Next.js team con…