I have a form in a modal that I want to close right after submitting. The form works perfectly fine and the data input from the form gets reflected on the database, but the rest of the actions within the same method doesn't get triggered for some reason.
I want the form to execute when the submit button is pressed :
1) change the loading
to true through `setLoading(true)
2) post the input data
3) close the modal
Currenly, 1) and 3) aren't being carried out. closeModal
works fine if I comment out the axios
.
Following is the modal:
const Form = ({ closeModal }) => {
const [email, setEmail] = useState('')
const [firstName, setFirstName] = useState('')
const [lastName, setLastName] = useState('')
const [loading, setLoading] = useState(false)
const handleSubmit = async e => {
setLoading(true)
e.preventDefault()
try {
await axios.post(`${ROOT_URL}/createEmailList`, {
email,
firstName,
lastName
})
} catch (err) {
console.log(err)
} finally {
setLoading(false)
closeModal()
}
}
return (
<form
className="form"
onSubmit={handleSubmit}
>
<div className="label">Name *</div>
<div className="desc">What is your name?</div>
<div className="input-container">
<label className="label">
<input
className="input"
type="text"
value={firstName}
onChange={e => setFirstName(e.target.value)}
/>
<div className="desc">First Name</div>
</label>
<label className="label">
<input
className="input"
type="text"
value={lastName}
onChange={e => setLastName(e.target.value)}
/>
<div className="desc">Last Name</div>
</label>
</div>
<div className="label">Email Address *</div>
<div className="input-container">
<label className="label">
<input
className="input2"
type="email"
value={email}
onChange={e => setEmail(e.target.value)}
required
/>
</label>
</div>
<button className="button">
{loading ? "Loading..." : "Submit"}
</button>
</form>
)
}
Also, I'm using Google Functions(Firebase) as the backend.
Update:
const handleSubmit = async e => {
setLoading(true)
e.preventDefault()
try {
await axios.post(`${ROOT_URL}/createEmailList`, {
email,
firstName,
lastName
})
setLoading(false)
closeModal()
} catch (err) {
console.log(err)
}
}