"use client"
import { useState } from 'react';
import {auth} from '../../firebase-config'
import {createUserWithEmailAndPassword} from 'firebase/auth'
import { useRouter } from 'next/router';
const SignUp = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleSubmit = (e) => {
const router = useRouter();
e.preventDefault();
// Handle form submission here (e.g., send data to server)
createUserWithEmailAndPassword(auth , email,password)
.then(()=>{
setEmail('');
setPassword('');
router.push('/')
}).catch((err)=>{
throw console.log(err);
})
}
I have used useEffect hook , windows.location.href , I have restarted my server but nothing works.
I want the user to redirect to landing page but it seems nothing is working.
2
Answers
In your current implementation, the
useRouter()
hook is being called inside thehandleSubmit
function, which is likely causing issues with the routing. In Next.js, hooks need to be called at the top level of your component, not within any callbacks or nested functions.Move this line
const router = useRouter();
right after importing it, calling it inside theSignUp
is restriciting its usage.router
needs to be accessable at the top level.