What is the main purpose of form submission handling in React?
To capture user input from a form and process it, such as saving data or triggering actions, while controlling the form behavior within React components.
Click to reveal answer
beginner
Which React hook is commonly used to manage form input values?
The useState hook is used to store and update form input values as the user types.
Click to reveal answer
beginner
Why do we use event.preventDefault() in a form submit handler?
It stops the browser from reloading the page when the form is submitted, allowing React to handle the data without losing the app state.
Click to reveal answer
beginner
How do you access the value of an input field in a React form?
By using the state variable linked to the input's value attribute and updating it with an onChange event handler.
Click to reveal answer
intermediate
What is a controlled component in React forms?
An input element whose value is controlled by React state, meaning React decides what the input shows and updates the state on user input.
Click to reveal answer
What does event.preventDefault() do in a React form submit handler?
AStops the page from reloading
BClears the form inputs
CSubmits the form to the server
DTriggers a browser alert
✗ Incorrect
Using event.preventDefault() prevents the default browser action of reloading the page on form submission.
Which hook is best to store form input values in React?
AuseRef
BuseEffect
CuseState
DuseContext
✗ Incorrect
useState is used to keep track of input values and update them as the user types.
How do you update the state when a user types in an input field?
AUsing the <code>onClick</code> event handler
BUsing the <code>onChange</code> event handler
CUsing the <code>onSubmit</code> event handler
DUsing the <code>onLoad</code> event handler
✗ Incorrect
The onChange event fires every time the input changes, allowing state updates.
What is a controlled component in React forms?
AAn input that manages its own state
BAn input that submits automatically
CAn input without any event handlers
DAn input managed by React state
✗ Incorrect
Controlled components have their value set by React state and update state on user input.
Why is it important to handle form submission in React instead of letting the browser do it?
ATo keep the app state and UI in sync without page reloads
BTo make the form submit faster
CTo disable the form inputs
DTo automatically clear the form
✗ Incorrect
Handling submission in React prevents page reloads and keeps the app interactive and stateful.
Explain how to handle a form submission in React step-by-step.
Think about how React controls inputs and what happens when you press submit.
You got /5 concepts.
Describe what a controlled component is and why it is useful in React forms.
Controlled means React decides what the input shows.
You got /4 concepts.
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?