'Don't know why the two functions are showing undefined in React
Why are the two functions in my react forms are showing undefined? How should i define them?
Line 10:5: 'formSubmit' is not defined no-undef
const Form = () => {
const [name,setName] = useState("");
const [email,setEmail] = useState("");
const [message,setMessage] = useState("");
const [sent,setSent] = useState(false);
formSubmit=(e)=>{
e.preventDefaults();
let data = {
name:name,
email:email,
message:message
}
}
Solution 1:[1]
You missed the keyword for declaring function
It should be
const formSubmit=(e)=>{
e.preventDefault();
let data = {
name:name,
email:email,
message:message
}
}
OR
function formSubmit(e) {
e.preventDefault();
let data = {
name: name,
email: email,
message: message,
};
}
Also preventDefaults is wrong, preventDefault is correct.
Solution 2:[2]
You should only add const before your formSubmit callback function
const Form = () => {
const [name,setName] = useState("");
const [email,setEmail] = useState("");
const [message,setMessage] = useState("");
const [sent,setSent] = useState(false);
const formSubmit=(e)=>{
e.preventDefaults();
let data = {
name:name,
email:email,
message:message
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David |
| Solution 2 |
