📖 Scenario: You are building a simple React web page with a form. When the user clicks the submit button, the page should not reload. Instead, you want to show a message that the form was submitted.
🎯 Goal: Create a React functional component with a form. Prevent the form's default submit behavior so the page does not reload. Show a message when the form is submitted.
📋 What You'll Learn
Create a React functional component named SubmitForm.
Add a form with a submit button inside the component.
Use an event handler function named handleSubmit for the form's onSubmit event.
Inside handleSubmit, prevent the default form submission behavior using event.preventDefault().
Show a message Form submitted! below the form when the submit button is clicked.
💡 Why This Matters
🌍 Real World
Preventing default form submission is common in web apps to handle data without reloading the page, improving user experience.
💼 Career
Understanding event handling and preventing default behavior is essential for frontend developers working with React or other frameworks.
Progress0 / 4 steps
1
Create the React component with a form
Create a React functional component named SubmitForm. Inside it, return a <form> element with a submit button labeled Submit.
React
Hint
Start by writing a function named SubmitForm that returns a form with a submit button.
2
Add the submit handler function
Inside the SubmitForm component, create a function named handleSubmit that takes an event parameter.
React
Hint
Define a function named handleSubmit inside the component that accepts event as a parameter.
3
Prevent the default form submission
Inside the handleSubmit function, call event.preventDefault() to stop the form from reloading the page when submitted.
React
Hint
Use event.preventDefault() inside handleSubmit to stop the page reload.
4
Connect the handler and show submission message
Add the onSubmit attribute to the <form> element and set it to {handleSubmit}. Also, add a React state variable named submitted initialized to false. Inside handleSubmit, set submitted to true. Below the form, conditionally render a <p> element with the text Form submitted! only when submitted is true.
React
Hint
Use React's useState to track submission. Attach handleSubmit to the form's onSubmit. Show the message only when submitted is true.
Practice
(1/5)
1. What does event.preventDefault() do in a React event handler?
easy
A. Removes the event listener from the element
B. Stops the browser's default action for that event
C. Triggers the event again immediately
D. Logs the event details to the console
Solution
Step 1: Understand the purpose of preventDefault()
This method stops the browser from performing its usual action, like submitting a form or following a link.
Step 2: Recognize what preventDefault() does not do
It does not remove listeners, re-trigger events, or log anything automatically.
Final Answer:
Stops the browser's default action for that event -> Option B
Confusing preventDefault with logging or debugging
2. Which of the following is the correct way to prevent a form submission's default behavior in React?
easy
A. function handleSubmit() { event.preventDefault(); }
B. function handleSubmit(event) { preventDefault(); }
C. function handleSubmit(event) { event.preventDefault(); }
D. function handleSubmit() { preventDefault(event); }
Solution
Step 1: Check the event parameter usage
The event object must be passed as a parameter to the handler to call preventDefault() on it.
Step 2: Verify the syntax of preventDefault call
Calling event.preventDefault() inside the handler with event parameter is correct syntax.
Final Answer:
function handleSubmit(event) { event.preventDefault(); } -> Option C
Quick Check:
Correct syntax requires event parameter and event.preventDefault() [OK]
Hint: Always include event parameter to call event.preventDefault() [OK]
Common Mistakes:
Not passing event parameter to the handler
Calling preventDefault without event prefix
Trying to call preventDefault without parentheses
3. What will be the output when clicking the link in this React component?
function Link() {
function handleClick(event) {
event.preventDefault();
alert('Link clicked!');
}
return <a href='https://example.com' onClick={handleClick}>Click me</a>;
}
medium
A. Only the alert 'Link clicked!' appears, no navigation happens
B. The browser navigates to https://example.com and shows alert
C. Nothing happens on click
D. The link navigates without showing alert
Solution
Step 1: Understand event.preventDefault() effect on link
Calling event.preventDefault() stops the browser from navigating to the href URL.
Step 2: Check what else happens on click
The alert('Link clicked!') runs, so the alert appears on click.
Final Answer:
Only the alert 'Link clicked!' appears, no navigation happens -> Option A
Quick Check:
preventDefault stops navigation, alert still runs [OK]
Hint: preventDefault stops navigation but lets your code run [OK]
Common Mistakes:
Expecting navigation to happen despite preventDefault
Thinking alert won't show if preventDefault is called
Confusing preventDefault with stopPropagation
4. Identify the error in this React event handler that tries to prevent form submission:
function handleSubmit() {
event.preventDefault();
console.log('Submitted');
}
medium
A. The function should return false instead of calling preventDefault
B. Using console.log inside the handler is not allowed
C. preventDefault() should be called after console.log
D. Missing event parameter in the function definition
Solution
Step 1: Check function parameters
The handler uses event.preventDefault() but does not receive event as a parameter, so event is undefined.
Step 2: Understand consequence of missing event
This causes a runtime error because event is not defined in the function scope.
Final Answer:
Missing event parameter in the function definition -> Option D
Quick Check:
Event object must be passed to handler to call preventDefault [OK]
Hint: Always include event parameter to access event.preventDefault() [OK]
Common Mistakes:
Forgetting to add event parameter
Thinking preventDefault can be called globally
Confusing preventDefault with returning false
5. You want to prevent a form from submitting only if a checkbox is unchecked. Which React event handler correctly implements this behavior?
function handleSubmit(event) {
// Prevent submission only if checkbox is unchecked
const checkbox = document.getElementById('agree');
if (/* condition here */) {
event.preventDefault();
alert('You must agree first');
}
}
hard
A. if (!checkbox.checked) { event.preventDefault(); alert('You must agree first'); }
B. if (checkbox.checked) { event.preventDefault(); alert('You must agree first'); }
C. if (checkbox.value === false) { event.preventDefault(); alert('You must agree first'); }
D. if (checkbox === null) { event.preventDefault(); alert('You must agree first'); }
Solution
Step 1: Check the checkbox condition for preventing submission
You want to prevent submission if the checkbox is NOT checked, so condition is !checkbox.checked.
Step 2: Confirm preventDefault is called only when checkbox is unchecked
Calling event.preventDefault() inside this condition stops submission correctly.
Final Answer:
if (!checkbox.checked) { event.preventDefault(); alert('You must agree first'); } -> Option A
Quick Check:
Prevent submission when checkbox is unchecked = !checkbox.checked [OK]
Hint: Use !checkbox.checked to detect unchecked state before preventDefault [OK]
Common Mistakes:
Using checkbox.checked instead of !checkbox.checked
Checking checkbox.value instead of checked property
Checking if checkbox is null instead of its checked state
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.