LearnThatStack Ace your next interview
React · question
Question 14 of 103

How do you prevent default behavior in React events?

beginner
← All React questions
Re-explain

Call preventDefault() on the event object that React passes to your handler. The usual case is a form, where it stops the browser from submitting and reloading the page, so your code can handle the data instead.

function Form() {
  const handleSubmit = (event) => {
    event.preventDefault(); // Prevents form from submitting
    console.log('Form submitted');
  };
  
  return (
    <form onSubmit={handleSubmit}>
      <input type="text" />
      <button type="submit">Submit</button>
    </form>
  );
}

Returning false from the handler doesn't work in React the way it does in plain HTML. You must call preventDefault() explicitly. To stop the event from bubbling up to parent elements, call stopPropagation(), which is a different thing.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for React One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • What's the difference between preventDefault() and stopPropagation(), and when would you call both? preventDefault cancels the browser's own action. stopPropagation keeps parent handlers from seeing the event. Neither one does the other's job.
  • Returning false cancels the default in plain HTML. Why doesn't it work in a React handler? React ignores what the handler returns. The return-false trick belongs to inline HTML handlers, so in React you call preventDefault instead.
  • What exactly is the event object React passes to your handler? A SyntheticEvent that wraps the browser's native event and has the same interface. event.nativeEvent gives you the underlying event.
  • How do you pass an item id to a handler and still have access to the event? Wrap the call in an arrow function, like onClick={(e) => handleDelete(id, e)}, and pass the event along yourself.
  • Once you've stopped the submit, how do you read the form's values? Controlled inputs already hold their values in state. For uncontrolled inputs, read them with new FormData(event.currentTarget).

What you can say

  1. You call preventDefault() on the event object React passes into your handler, and that stops the browser from doing whatever it would do on its own.
  2. The usual case is a form, because by default the browser submits it and reloads the page before your code gets a chance to handle the data.
  3. So the first line of my onSubmit handler is event.preventDefault(), and after that I read the values and send them wherever they need to go.
  4. A common slip is returning false from the handler, which works in plain HTML but does nothing in React, so you have to call preventDefault() yourself.
  5. It's also separate from stopPropagation(), which stops the event from bubbling up to parent handlers but doesn't stop the browser's default action.

Weak answers to avoid

  • Returns false to cancel the submit React ignores the handler's return value, so the page still reloads. Say that you call event.preventDefault() yourself, and that the HTML habit doesn't carry over.
  • Treats stopPropagation as the same thing stopPropagation only keeps the event from reaching parent handlers. The form still submits and the page reloads. Say what each one stops.
  • Calls preventDefault after an await By then the event has finished dispatching and the browser has already started the submit. Call it first, synchronously, then start the async work.
  • Names the method but not the problem it solves Saying 'call preventDefault' without the form-reload case sounds memorized. Say what the browser does by default and why your handler has to stop it.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the React cheatsheet.

← Back to all React questions
Pro · $10/mo

88 of 103 React answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime