Sometimes, when you click a link or submit a form, the browser does something automatically. Preventing default behavior stops that automatic action so you can control what happens next.
Preventing default behavior in React
function handleEvent(event) { event.preventDefault(); // your code here }
The event object is passed automatically to event handlers in React.
Calling event.preventDefault() stops the browser's default action for that event.
function handleClick(event) { event.preventDefault(); alert('Link clicked but no page change!'); }
function handleSubmit(event) { event.preventDefault(); console.log('Form submitted without page reload'); }
This React component shows a link and a form. Clicking the link or submitting the form will not trigger the browser's default actions. Instead, alerts appear to confirm the events were caught and handled.
Accessibility is considered by adding an aria-label to the link. The layout uses semantic HTML and spacing for clarity.
import React from 'react'; export default function PreventDefaultExample() { function handleLinkClick(event) { event.preventDefault(); alert('You clicked the link, but no page change!'); } function handleFormSubmit(event) { event.preventDefault(); alert('Form submitted without reload!'); } return ( <main> <h1>Prevent Default Behavior Example</h1> <a href="https://example.com" onClick={handleLinkClick} aria-label="Example link">Example Link</a> <form onSubmit={handleFormSubmit} style={{ marginTop: '1rem' }}> <button type="submit">Submit Form</button> </form> </main> ); }
Always pass the event object to your handler to use preventDefault().
Preventing default behavior is useful to create custom interactions without page reloads or navigation.
Use event.preventDefault() to stop the browser's automatic action.
This lets you control what happens when users click links or submit forms.
It helps create smoother, interactive web apps without page reloads.
Practice
event.preventDefault() do in a React event handler?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 BQuick Check:
preventDefault() = Stops default browser action [OK]
- Thinking preventDefault removes event listeners
- Assuming preventDefault triggers the event again
- Confusing preventDefault with logging or debugging
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 CQuick Check:
Correct syntax requires event parameter and event.preventDefault() [OK]
- Not passing event parameter to the handler
- Calling preventDefault without event prefix
- Trying to call preventDefault without parentheses
function Link() {
function handleClick(event) {
event.preventDefault();
alert('Link clicked!');
}
return <a href='https://example.com' onClick={handleClick}>Click me</a>;
}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 AQuick Check:
preventDefault stops navigation, alert still runs [OK]
- Expecting navigation to happen despite preventDefault
- Thinking alert won't show if preventDefault is called
- Confusing preventDefault with stopPropagation
function handleSubmit() {
event.preventDefault();
console.log('Submitted');
}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 DQuick Check:
Event object must be passed to handler to call preventDefault [OK]
- Forgetting to add event parameter
- Thinking preventDefault can be called globally
- Confusing preventDefault with returning false
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');
}
}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 AQuick Check:
Prevent submission when checkbox is unchecked = !checkbox.checked [OK]
- Using checkbox.checked instead of !checkbox.checked
- Checking checkbox.value instead of checked property
- Checking if checkbox is null instead of its checked state
Ready to prove it?
Pass the test and this pattern moves unknown
