I tried to create authentication on react, but now I am currently stuck as my try and catch block is not working. When I click the signup button, I am not getting any error nor any response from the site. No user is uploaded to the Firebase database.
The Code is Given Below.
import React,{useRef,useState} from 'react'
import {Form,Button,Card,Alert} from 'react-bootstrap'
import {useAuth} from '../Context/AuthContext'
function Signup() {
const emailRef=useRef()
const passwordRef=useRef()
const passwordConfirmRef=useRef( )
const {signup} =useAuth();
const [error,setError]=useState();
const [loading,setLoading]=useState(false);
async function handleSubmit(e){
e.preventDefault()
if(passwordRef.current.value!==passwordConfirmRef.current.value){
return setError("Passwords Do Not Match")
}
try{
setError("");
setLoading(true);
await signup(emailRef.current.value,passwordRef.current.value)
}
catch {setError("Failed To Create An Account")}
setLoading(false);
}
}
export default Signup
2
Answers
The Submit Button Type Was Not Mentioned In The Above Code. So It Was Not Submitting And Hence No Error Were Shown In The Console. So Just Add
type='submit'
To The Submit Button And The Code Will Work Properly.Try catch block looks like this:
Are you sure you paste correct code ?
Signup()
don’t have closing brackets. I don’t see in your code that you’re importingsignup()
function. And your main function is namedSignup()
this is not a good practice. A good name for your function can beonSignUp()
instead ofSignup()
.