preventDefault Preventing Default Behavior

Last updated: 2026-11-02

preventDefault Preventing Default Behavior

e.preventDefault() stops the browser's default action for an event, most famously the full page reload caused by submitting a form. React forms almost always call it first inside onSubmit and then handle the data through controlled state, which gives you a submission without a page refresh. It is a one-line call, but forgetting it is one of the most common React form bugs.

GroupRendering & State
KindAPI
SinceEarliest React versions

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

function handleSubmit(e) {
  e.preventDefault() // Stops the browser's default action (such as reloading the page)
  // Handle the submission with state here
}
<form onSubmit={handleSubmit}>

⚙️ Parameters

e (event object)The React synthetic event; preventDefault can only be called while the handler runs synchronously, so it cannot be stored and called later.

Returns:No return value (undefined); after the call the browser's default action for that event no longer happens.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

A search form: type a keyword and click "Search" and the page does not reload, while "Searched: xxx" appears below; submitting an empty value asks for a keyword instead — the default full page reload is successfully prevented.

💡 Key Points

⚠️ Warning Preventing the page reload relies on e.preventDefault(); returning false does nothing at all in React.
💡 Tip If a plain button inside a form should not submit it, giving it type="button" also avoids the default behavior.
ℹ️ Info preventDefault stops the default action while stopPropagation stops the event from bubbling; the two are independent and you pick the one you need.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Why does submitting a React form reload the page?
A Because the browser has a default action for form submissions. Call e.preventDefault() on the first line of the onSubmit handler to block the reload and process the data with state instead.
Q What does e.preventDefault() do?
A It cancels the browser's default action for the event: the form does not reload, an anchor does not navigate, the context menu does not appear. It only cancels the default action and does not stop the event from propagating.
Q What is the difference between preventDefault and stopPropagation?
A The first blocks the "default action" (such as reloading the page), the second blocks the event from continuing to bubble up to parent elements; they do not affect each other and can be called together.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏