0
0
Reactframework~20 mins

Handling events in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Event Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What happens when the button is clicked?
Consider this React component. What will be the displayed text after clicking the button once?
React
import React, { useState } from 'react';

function ClickCounter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

export default ClickCounter;
AThe button click causes a syntax error.
BThe text remains 'You clicked 0 times'.
CThe text changes to 'You clicked 1 times'.
DThe text changes to 'You clicked NaN times'.
Attempts:
2 left
💡 Hint
Think about how useState updates the count when the button is clicked.
📝 Syntax
intermediate
1:00remaining
Which option correctly attaches an event handler in React?
You want to run a function named handleClick when a button is clicked. Which JSX syntax is correct?
A<button onClick={handleClick}>Click</button>
B<button onClick={handleClick()}>Click</button>
C<button onclick={handleClick}>Click</button>
D<button OnClick={handleClick}>Click</button>
Attempts:
2 left
💡 Hint
React event handlers are camelCase and expect a function reference, not a function call.
🔧 Debug
advanced
2:00remaining
Why does this event handler cause an error?
This React component throws an error when clicking the button. What is the cause?
React
import React from 'react';

function MyButton() {
  function handleClick() {
    console.log(this);
  }
  return <button onClick={handleClick}>Click me</button>;
}

export default MyButton;
AThe component is missing a return statement.
B'this' is undefined inside handleClick because it is a regular function, not bound to the component.
ChandleClick is called immediately instead of on click, causing an error.
DThe onClick attribute is misspelled causing the handler not to run.
Attempts:
2 left
💡 Hint
Consider how 'this' behaves in JavaScript functions inside React components.
state_output
advanced
2:00remaining
What is the output after clicking the button twice quickly?
Examine this React component. What will be the final count displayed after clicking the button twice quickly?
React
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  function increment() {
    setCount(count + 1);
    setCount(count + 1);
  }
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

export default Counter;
ACount will be 1 after two quick clicks.
BCount will be 2 after two quick clicks.
CCount will be 4 after two quick clicks.
DCount will be 0 after two quick clicks.
Attempts:
2 left
💡 Hint
Think about how React batches state updates and the value of 'count' inside increment.
🧠 Conceptual
expert
2:30remaining
Why use event delegation in React event handling?
React uses a synthetic event system that attaches a single event listener at the root. What is the main benefit of this event delegation approach?
AIt disables event bubbling to avoid unwanted side effects.
BIt allows direct access to native DOM events without any abstraction.
CIt forces all events to be handled synchronously, preventing delays.
DIt improves performance by reducing the number of event listeners attached to DOM nodes.
Attempts:
2 left
💡 Hint
Think about how many event listeners would be needed if each element had its own listener.