Before building the form, initialize your React environment.
npx create-react-app enquiry-app cd enquiry-app npm start
src/App.js → Your main form code
src/index.js → The entry point
In React, an Enquiry Form is a "Controlled Component" where user data (Name, Email, Message) is synced with the component's State.
Rule 1: State Storage - Every input field must have a corresponding state variable.
Rule 2: onChange - Use the onChange event to capture every keystroke.
Rule 3: preventDefault - Essential! In handleSubmit, use e.preventDefault() to stop the page from refreshing.
// The most important line in React Forms:
function handleSubmit(e) {
e.preventDefault();
// process data here...
}
import { useState } from "react";
function App() {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
alert(`Submitted: ${name} (${email})`);
};
return (
<form onSubmit={handleSubmit}>
<input type="text" placeholder="Name"
onChange={(e) => setName(e.target.value)} />
<input type="email" placeholder="Email"
onChange={(e) => setEmail(e.target.value)} />
<button type="submit">Submit</button>
</form>
);
}
| Type | Description | Best For |
|---|---|---|
| Controlled | React State manages the value | Most forms (Validation, instant feedback) |
| Uncontrolled | DOM handles the value (using Ref) | Simple forms, non-React integration |
Controlled components are the gold standard in React development.
| Feature | Traditional HTML | React |
|---|---|---|
| Reload | Forces page refresh | No refresh (SPA) |
| Validation | HTML5 attributes only | Custom JS logic (Immediate) |
| Submission | Sent to action URL | Handled by JS function |
Once you capture the form data in your handleSubmit function, you typically send it to:
State → Events → Hooks → Forms (Complete)
↓
Next: API Integration → Connecting to a Database