Prompt
build a form in react
Latest observation
Sep 20, 2026
OpenAI APIWeb search: off
Sure — here’s a simple React form example with controlled inputs and basic submit handling.
Example: Simple Contact Form
import React, { useState } from "react";
function ContactForm() {
const [formData, setFormData] = useState({
name: "",
email: "",
message: "",
});
const handleChange = (e) => {
const { name, value } = e.target;
setFormData((prev) => ({
...prev,
[name]: value,
}));
};
const handleSubmit = (e) => {
e.preventDefault();
console.log("Form submitted:", formData);
alert(`Submitted!\nName: ${formData.name}\nEmail: ${formData.email}`);
};
return (
<form onSubmit={handleSubmit} style={styles.form}>
<h2>Contact Us</h2>
<label style={styles.label}>
Name
<input
type="text"
name="name"
value={formData.name}
onChange={handleChange}
style={styles.input}
required
/>
</label>
<label style={styles.label}>
Email
<input
type="email"
name="email"
value={formData.email}
onChange={handleChange}
style={styles.input}
required
/>
</label>
<label style={styles.label}>
Message
<textarea
name="message"
value={formData.message}
onChange={handleChange}
style={styles.textarea}
required
/>
</label>
<button type="submit" style={styles.button}>
Submit
</button>
</form>
);
}
const styles = {
form: {
maxWidth: "400px",
margin: "0 auto",
padding: "20px",
border: "1px solid #ddd",
borderRadius: "8px",
display: "flex",
flexDirection: "column",
gap: "16px",
fontFamily: "Arial, sans-serif",
},
label: {
display: "flex",
flexDirection: "column",
gap: "6px",
fontWeight: "bold",
},
input: {
padding: "10px",
borderRadius: "4px",
border: "1px solid #ccc",
},
textarea: {
padding: "10px",
borderRadius: "4px",
border: "1px solid #ccc",
minHeight: "100px",
},
button: {
padding: "10px 14px",
border: "none",
borderRadius: "4px",
backgroundColor: "#007bff",
color: "white",
cursor: "pointer",
},
};
export default ContactForm;
Use it in your app
import ContactForm from "./ContactForm";
function App() {
return <ContactForm />;
}
export default App;
If you want, I can also build:
- a form with validation
- a login/signup form
- a form using
FormikorReact Hook Form
Brands