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

Error: React Hook "useRouter" is called conditionally. React Hooks must be called in the exact same order in every component render. #344

Open
DanielHirunrusme opened this issue Jun 4, 2024 · 1 comment · May be fixed by #355

Comments

@DanielHirunrusme
Copy link

DanielHirunrusme commented Jun 4, 2024

The error below is being thrown on every auth route component (i.e https://github.com/vercel/nextjs-subscription-payments/blob/main/components/ui/AuthForms/Signup.tsx)

Error: React Hook "useRouter" is called conditionally. React Hooks must be called in the exact same order in every component render.

It's due to this line of code:
const router = redirectMethod === 'client' ? useRouter() : null;

If you want to provide the option of redirecting based on server/client should this const instead be assigned with a useEffect/useState?

Or would it be safe just to assigned it as:
const router = useRouter()

@MartinSWDev
Copy link

MartinSWDev commented Jun 30, 2024

I found just separating out the ternary and replacing the router uses where the error highlights fixed this :
const router = useRouter();
const routerMethod = redirectMethod === 'client' ? router : null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants