0
0
Reactframework~20 mins

Passing arguments to handlers in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Argument Passing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when clicking the button?

Consider this React component. What will be logged to the console when the button is clicked?

React
import React, { useState } from 'react';

function ClickLogger() {
  const [count, setCount] = useState(0);

  function handleClick(value) {
    console.log(`Clicked with value: ${value}`);
    setCount(count + value);
  }

  return (
    <button onClick={() => handleClick(5)}>
      Click me
    </button>
  );
}

export default ClickLogger;
ALogs 'Clicked with value: 5' and updates count to 5
BLogs 'Clicked with value: 0' and updates count to 0
CThrows a TypeError because handleClick is called incorrectly
DLogs 'Clicked with value: undefined' and count remains 0
Attempts:
2 left
💡 Hint

Look at how the argument is passed inside the onClick handler.

📝 Syntax
intermediate
2:00remaining
Which option correctly passes an argument to the event handler?

Which of the following is the correct way to pass the argument 10 to the handleClick function in React?

React
function MyComponent() {
  function handleClick(num) {
    console.log(num);
  }

  return (
    <button /* choose correct onClick here */>
      Press
    </button>
  );
}
A<button onClick={() => handleClick(10)}>Press</button>
B<button onClick={handleClick(10)}>Press</button>
C<button onClick={handleClick}>Press</button>
D<button onClick={handleClick.bind(null, 10)}>Press</button>
Attempts:
2 left
💡 Hint

Remember that calling the function directly in JSX runs it immediately.

🔧 Debug
advanced
2:00remaining
Why does this code cause an error when clicking the button?

Examine the code below. Clicking the button causes an error. What is the cause?

React
import React from 'react';

function Counter() {
  let count = 0;

  function increment(step) {
    count += step;
    console.log(count);
  }

  return (
    <button onClick={increment(1)}>
      Add 1
    </button>
  );
}

export default Counter;
AThe button lacks an event parameter in the handler, causing a TypeError
BThe count variable is not a state, so React cannot update the UI
Cincrement is not bound to the component, causing 'this' to be undefined
DThe onClick calls increment immediately on render, causing an error
Attempts:
2 left
💡 Hint

Check when the function inside onClick is executed.

state_output
advanced
2:00remaining
What is the value of count after clicking the button twice?

Given this React component, what will be the value of count after clicking the button two times?

React
import React, { useState } from 'react';

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

  function increment(step) {
    setCount(count + step);
  }

  return (
    <button onClick={() => increment(1)}>
      Increment
    </button>
  );
}

export default Counter;
ANaN
B1
C2
D0
Attempts:
2 left
💡 Hint

Remember how React batches state updates and closures capture values.

🧠 Conceptual
expert
2:00remaining
Why use an arrow function to pass arguments to event handlers in React?

Which explanation best describes why we often use arrow functions like () => handleClick(arg) when passing arguments to event handlers?

ATo convert the handler into a pure function without side effects
BBecause React requires all event handlers to be arrow functions for binding
CTo prevent the handler from running immediately during render and instead run it only on the event trigger
DTo automatically bind the handler to the component's <code>this</code> context
Attempts:
2 left
💡 Hint

Think about when the function should run in React.