0
0
Reactframework~10 mins

Preventing default behavior in React - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to prevent the default form submission behavior.

React
function MyForm() {
  function handleSubmit(event) {
    event.[1]();
    alert('Form submitted!');
  }

  return (
    <form onSubmit={handleSubmit}>
      <button type="submit">Submit</button>
    </form>
  );
}
Drag options to blanks, or click blank then click option'
ApreventDefault
Bhalt
CstopPropagation
Dcancel
Attempts:
3 left
💡 Hint
Common Mistakes
Using stopPropagation instead of preventDefault
Trying to return false instead of calling preventDefault
2fill in blank
medium

Complete the code to prevent the default link navigation when clicking the anchor tag.

React
function Link() {
  function handleClick(event) {
    event.[1]();
    alert('Link clicked without navigation');
  }

  return <a href="https://example.com" onClick={handleClick}>Click me</a>;
}
Drag options to blanks, or click blank then click option'
AstopPropagation
BpreventDefault
CcancelBubble
Dhalt
Attempts:
3 left
💡 Hint
Common Mistakes
Using stopPropagation which only stops event bubbling
Not calling any method and expecting the link not to navigate
3fill in blank
hard

Fix the error in the event handler to correctly prevent default behavior.

React
function Button() {
  function handleClick(event) {
    event.[1]();
    alert('Button clicked');
  }

  return <button onClick={handleClick}>Click me</button>;
}
Drag options to blanks, or click blank then click option'
ApreventDefault
BstopPropagation()
CpreventDefault()
DstopPropagation
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses and writing event.preventDefault instead of event.preventDefault()
Using stopPropagation instead of preventDefault
4fill in blank
hard

Fill both blanks to prevent default behavior and stop event bubbling in the click handler.

React
function CustomButton() {
  function handleClick(event) {
    event.[1]();
    event.[2]();
    alert('Clicked without default or bubbling');
  }

  return <button onClick={handleClick}>Click me</button>;
}
Drag options to blanks, or click blank then click option'
ApreventDefault
BstopPropagation
CcancelBubble
Dhalt
Attempts:
3 left
💡 Hint
Common Mistakes
Using cancelBubble which is not a method but a property
Calling only one of the methods and expecting both behaviors
5fill in blank
hard

Fill all three blanks to prevent default, stop propagation, and log the event type.

React
function LoggerButton() {
  function handleClick(event) {
    event.[1]();
    event.[2]();
    console.log(event.[3]);
  }

  return <button onClick={handleClick}>Click me</button>;
}
Drag options to blanks, or click blank then click option'
ApreventDefault
BstopPropagation
Ctype
Dtarget
Attempts:
3 left
💡 Hint
Common Mistakes
Logging event.target instead of event.type
Not calling the methods with parentheses