0
0
Reactframework~20 mins

Preventing default behavior in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prevent Default Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when clicking the link in this React component?
Consider this React component with a link. What will happen when the user clicks the link?
React
import React from 'react';

export default function LinkComponent() {
  function handleClick(event) {
    event.preventDefault();
    alert('Link clicked but default prevented');
  }

  return (
    <a href="https://example.com" onClick={handleClick} aria-label="Example link">
      Go to example.com
    </a>
  );
}
AThe alert shows but the browser does NOT navigate to https://example.com.
BThe browser navigates to https://example.com and shows the alert.
CThe browser navigates to https://example.com without showing the alert.
DNothing happens when clicking the link.
Attempts:
2 left
💡 Hint
Think about what event.preventDefault() does in a click event on a link.
state_output
intermediate
2:00remaining
What is the button's label after clicking it once?
This React component toggles a button label on click. What will the button show after the first click?
React
import React, { useState } from 'react';

export default function ToggleButton() {
  const [clicked, setClicked] = useState(false);

  function handleClick(event) {
    event.preventDefault();
    setClicked(!clicked);
  }

  return (
    <button onClick={handleClick} aria-pressed={clicked}>
      {clicked ? 'Clicked!' : 'Click me'}
    </button>
  );
}
AThe button label changes to 'Clicked!'.
BThe button label stays 'Click me'.
CThe button label disappears.
DThe button becomes disabled.
Attempts:
2 left
💡 Hint
Check what happens to the state when the button is clicked and default is prevented.
📝 Syntax
advanced
2:00remaining
Which option correctly prevents form submission in React?
You want to prevent a form from submitting and refreshing the page. Which onSubmit handler is correct?
React
function Form() {
  function handleSubmit(event) {
    // prevent form submission
  }

  return (
    <form onSubmit={handleSubmit} aria-label="Sample form">
      <button type="submit">Submit</button>
    </form>
  );
}
Afunction handleSubmit(event) { event.stopPropagation(); console.log('Submitted'); }
Bfunction handleSubmit(event) { event.preventDefault(); console.log('Submitted'); }
Cfunction handleSubmit() { event.preventDefault(); console.log('Submitted'); }
Dfunction handleSubmit(event) { return false; console.log('Submitted'); }
Attempts:
2 left
💡 Hint
Remember the event object must be passed and preventDefault called on it.
🔧 Debug
advanced
2:00remaining
Why does this React form still submit despite calling preventDefault?
Look at this code. The form submits and reloads the page even though preventDefault is called. Why?
React
function MyForm() {
  function handleSubmit(event) {
    event.preventDefault();
    alert('Form submitted');
  }

  return (
    <form onSubmit={handleSubmit} aria-label="Debug form">
      <button type="submit">Submit</button>
    </form>
  );
}
AThe form needs a method attribute to prevent submission.
BpreventDefault is called too late after alert, so form submits anyway.
CThe button type should be 'button' not 'submit' to prevent submission.
DThe event object is not passed to handleSubmit, so event is undefined causing an error.
Attempts:
2 left
💡 Hint
Check the function parameters and usage of event inside handleSubmit.
🧠 Conceptual
expert
2:00remaining
What is the main reason to use event.preventDefault() in React event handlers?
Why do React developers use event.preventDefault() in event handlers like onClick or onSubmit?
ATo disable the event handler so it runs only once.
BTo stop React from re-rendering the component after the event.
CTo stop the browser's default action so React can handle the event without page reload or navigation.
DTo automatically stop propagation of the event to parent elements.
Attempts:
2 left
💡 Hint
Think about what happens by default when clicking links or submitting forms in a browser.