Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
179
rated 0 times [  183] [ 4]  / answers: 1 / hits: 10996  / 3 Years ago, sun, april 18, 2021, 12:00:00

I am trying to use the event.preventDefault() method but I am continuously receiving error. It says that event has been deprecated.


I am making a Firebase SignUp form and I want to prevent the form from Submitting.


Here is the complete code.


import React from "react"
import styled from "styled-components"
import getFirebase from "../../firebase"
import useInput from "./useInput"

const SignUpForm = () => {
const firebaseInstance = getFirebase()
const email = useInput("")
const password = useInput("")

const signUp = async () => {
event.preventDefault()
try {
if (firebaseInstance) {
const user = await firebaseInstance
.auth()
.createUserWithEmailAndPassword(email.value, password.value)
console.log("user", user)
alert(`Welcome ${email.value}!`)
}
} catch (error) {
console.log("error", error)
alert(error.message)
}
}
event.preventDefault()
return (
<FormWrapper onSubmit={() => signUp()}>
<Title>Sign up</Title>
<Input placeholder="Email" {...email} />
<Input placeholder="Password" type="password" {...password} />
<Button type="submit">Sign up</Button>
</FormWrapper>
)
}

export default SignUpForm


And the useInput code:


import { useState } from "react"

const useInput = initialValue => {
const [value, setValue] = useState(initialValue)

const handleChange = event => {
setValue(event.target.value)
}

return {
value,
onChange: handleChange,
}
}

export default useInput

More From » forms

 Answers
4

What that warning means is that the global variable window.event is deprecated. You can still access the event associated with an event handler, you just have to go about it the proper way - by using the parameter from the handler callback.


Change


<FormWrapper onSubmit={() => signUp()}>

to


<FormWrapper onSubmit={signUp}>

and then signUp's first parameter will be the event, and you'll be able to use it and call preventDefault on it as you're trying.


const signUp = async (event) => {

But don't put event.preventDefault() in your functional component's main body - that is, it shouldn't be here:


event.preventDefault()
return (
...

Only put it inside the signUp handler.


[#1463] Saturday, April 10, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dustin

Total Points: 599
Total Questions: 105
Total Answers: 106

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
dustin questions
;