0
0
Reactframework~20 mins

Synthetic events in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Synthetic Events Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a React synthetic event is reused asynchronously?

Consider this React code snippet where a synthetic event is accessed inside a setTimeout callback:

function Button() {
  function handleClick(event) {
    setTimeout(() => {
      alert(event.type);
    }, 1000);
  }
  return <button onClick={handleClick}>Click me</button>;
}

What will the alert show when the button is clicked?

React
function Button() {
  function handleClick(event) {
    setTimeout(() => {
      alert(event.type);
    }, 1000);
  }
  return <button onClick={handleClick}>Click me</button>;
}
AIt alerts 'undefined' because the synthetic event is nullified.
BIt throws a runtime error because event is undefined.
CIt alerts 'click' as expected.
DIt alerts 'button' because event.target is used instead of event.type.
Attempts:
2 left
💡 Hint

Remember that React synthetic events are pooled and reused for performance.

📝 Syntax
intermediate
1:30remaining
Which option correctly prevents default behavior in a React synthetic event?

Given a React event handler, which code correctly prevents the default action of the event?

React
function handleSubmit(event) {
  // prevent default here
}
Aevent.defaultPrevented = true;
Bevent.preventDefault();
Cevent.stopPropagation();
Dreturn false;
Attempts:
2 left
💡 Hint

Think about the standard method to stop default browser actions in React events.

🔧 Debug
advanced
2:30remaining
Why does this React synthetic event handler cause a TypeError?

Examine this React component code:

function Input() {
  function handleChange(event) {
    const value = event.target.value.toUpperCase();
    setInputValue(value);
  }
  const [inputValue, setInputValue] = React.useState('');
  return <input value={inputValue} onChange={handleChange} />;
}

Sometimes, this code throws TypeError: Cannot read property 'value' of null. Why?

React
function Input() {
  function handleChange(event) {
    const value = event.target.value.toUpperCase();
    setInputValue(value);
  }
  const [inputValue, setInputValue] = React.useState('');
  return <input value={inputValue} onChange={handleChange} />;
}
ABecause the synthetic event is reused and event.target becomes null before accessing value.
BBecause setInputValue is called before event.target.value is defined.
CBecause the input element is uncontrolled and causes null target.
DBecause toUpperCase() is called on a number instead of a string.
Attempts:
2 left
💡 Hint

Think about React's event pooling and when event properties are accessible.

🧠 Conceptual
advanced
2:00remaining
How does React's synthetic event system improve performance?

React uses a synthetic event system instead of native browser events. Which explanation best describes how this improves performance?

ABy attaching event listeners to every DOM node individually, React speeds up event handling.
BBy converting all events to native DOM events, React avoids extra processing.
CBy pooling event objects and reusing them, React reduces memory allocation and garbage collection overhead.
DBy disabling event bubbling, React prevents unnecessary event propagation.
Attempts:
2 left
💡 Hint

Think about how reusing objects can save resources.

state_output
expert
3:00remaining
What is the final state value after this React synthetic event handler runs?

Analyze this React component:

function Counter() {
  const [count, setCount] = React.useState(0);

  function handleClick(event) {
    event.persist();
    setTimeout(() => {
      setCount(count + 1);
      console.log(event.type);
    }, 1000);
  }

  return <button onClick={handleClick}>Count: {count}</button>;
}

After clicking the button once, what will be the value of count shown on the button after 1 second?

React
function Counter() {
  const [count, setCount] = React.useState(0);

  function handleClick(event) {
    event.persist();
    setTimeout(() => {
      setCount(count + 1);
      console.log(event.type);
    }, 1000);
  }

  return <button onClick={handleClick}>Count: {count}</button>;
}
A0
B2
CNaN
D1
Attempts:
2 left
💡 Hint

Consider how closures capture state values in React hooks.