I have a function written to authenticate and signup a user on my react app. If successful, the function gives an alert message saying "Account Created". I have given the function a setTimeout function which should execute in 1 second and send the user to their profile. The setTimeout is simply not executing ( everything else in the function is). When I remove the setTimeout function and just send the user to their profile without any timed delay, it works properly. What is wrong with they way I'm trying to execute this?
Heres my code in my AuthContext:
import { useHistory } from 'react-router-dom'
const history = useHistory()
function authSignup(email, password) {
auth.createUserWithEmailAndPassword(email, password)
.then(() => {
setMessage("Account Created")
setSuccess(true)
setLoading(false)
setTimeout(() => history.push('/'), 1000)
})
.catch(function (error) {
let errorCode = error.code;
let errorMessage = error.message;
if (errorCode) {
setError(errorMessage);
setLoading(false)
}
})
}
And this function is being executed on a form submit:
const submitForm = async (e) => {
e.preventDefault()
setError('')
setMessage('')
if (password !== passwordConfirm) {
return setError("Passwords do not match")
}
await authSignup(email, password)
}