📖 Scenario: You are building a simple contact form on a website. Visitors can type their name and email, then submit the form.
🎯 Goal: Create a React functional component that shows a form with two input fields: one for name and one for email. When the user submits the form, the component should save the entered data in state and clear the inputs.
📋 What You'll Learn
Use React functional components with hooks
Create controlled input fields for name and email
Handle form submission with a function called handleSubmit
Store submitted data in a state variable called submittedData
Clear the input fields after submission
💡 Why This Matters
🌍 Real World
Forms are everywhere on websites and apps. Handling form submission correctly is essential for collecting user input like contact info, feedback, or orders.
💼 Career
Knowing how to build and manage forms in React is a key skill for frontend developers. It shows you can handle user input, state, and events effectively.
Progress0 / 4 steps
1
Set up initial state for form inputs
Create a React functional component called ContactForm. Inside it, use useState to create two state variables: name and email, both initially empty strings.
React
Hint
Use const [name, setName] = useState('') and similarly for email.
2
Add a state variable for submitted data
Inside the ContactForm component, create a state variable called submittedData using useState. It should start as null.
React
Hint
Use const [submittedData, setSubmittedData] = useState(null) to hold the form submission.
3
Create the form with controlled inputs and submit handler
Inside the ContactForm component, return a form element. Add two controlled input fields: one for name and one for email. Use value and onChange to link them to state. Add a handleSubmit function that prevents default submission, sets submittedData to an object with name and email, and clears the name and email states. Attach handleSubmit to the form's onSubmit event.
React
Hint
Remember to prevent the default form submission. Use controlled inputs with value and onChange. Clear inputs after submission.
4
Display submitted data below the form
Below the form in the ContactForm component, add a conditional rendering that shows a div with the text Name: {submittedData.name} and Email: {submittedData.email} only if submittedData is not null.
React
Hint
Use {submittedData && ( ... )} to show the data only if it exists. Use paragraphs to show name and email.
Practice
(1/5)
1. What does event.preventDefault() do in a React form submission handler?
easy
A. It automatically submits the form data to the server.
B. It stops the browser from reloading the page when the form is submitted.
C. It clears all the input fields in the form.
D. It resets the component's state to initial values.
Solution
Step 1: Understand default form behavior
By default, submitting a form reloads the page to send data to the server.
Step 2: Role of event.preventDefault()
This method stops the default reload so you can handle submission in React code.
Final Answer:
It stops the browser from reloading the page when the form is submitted. -> Option B
Quick Check:
event.preventDefault() = stops reload [OK]
Hint: Remember: preventDefault stops page reload on submit [OK]
Common Mistakes:
Thinking preventDefault clears inputs
Assuming preventDefault submits data automatically
Confusing preventDefault with resetting state
2. Which of the following is the correct way to attach a submit handler to a form in React?
easy
A.
B.
C.
D.
Solution
Step 1: Check React event naming conventions
React uses camelCase for event handlers, so it must be onSubmit.
Step 2: Validate JSX syntax
Attributes are case-sensitive; onsubmit is incorrect, must be onSubmit.
Final Answer:
<form onSubmit={handleSubmit}> -> Option A
Quick Check:
React event handlers use camelCase = onSubmit [OK]
Hint: React events use camelCase like onSubmit, not lowercase [OK]
Common Mistakes:
Using lowercase onsubmit instead of onSubmit
Using non-existent attributes like submit or onsubmitHandler
Forgetting curly braces around handler function
3. What will be logged to the console when the form below is submitted?