We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- React
- Forms
- Contact Form
- Discussions
Contact Form
Contact Form
Sort by
recency
|
21 Discussions
|
Please Login in order to post a comment
import { useState } from "react"; import "./App.css";
import "h8k-components";
function App() { const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [message, setMessage] = useState(""); const [submittedData, setSubmittedData] = useState(null); const [error, setError] = useState("");
const handleSubmit = (e) => { e.preventDefault(); const isValid = name.trim()!=="" && email.trim()!=="" && message.trim()!=="";
};
return ( <>
Contact Form
setName(e.target.value)} placeholder="Name" data-testid="name-input" /> setEmail(e.target.value)} placeholder="Email" data-testid="email-input" /> setMessage(e.target.value)} placeholder="Message" data-testid="message-input" /> Submit {error && ( {error} )} {submittedData && (Submitted Information
Name: {submittedData.name}
Email: {submittedData.email}
Message: {submittedData.message}
)} ); }export default App;
React is such a powerful and flexible JavaScript library for building user interfaces! cricket betting exchange
Best Solution
const handleSubmit = (e) => { e.preventDefault(); if((name&&email&&message)==""){ setError("All fields are required.") } else{ setSubmittedData({name,email,message}) setName("") setEmail("") setMessage("") }
}
I don't believe this... I constantly kept failing the first test -> "displays error message when fields are empty and submit is clicked", and couldn't figure out what is wrong... in the end, I found the problem, and it seems I copied the error message without the dot (.) at the end, and that was causing the test to fail... aaggghhh