Bird
Raised Fist0
Reactframework~20 mins

Inline vs function handlers in React - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
React Event Handler Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when using inline event handlers in React?

Consider a React component that uses an inline arrow function as an event handler inside JSX. What is the main behavior difference compared to using a named function handler?

React
function Button() {
  const [count, setCount] = React.useState(0);
  return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>;
}
AInline handlers cause the component to lose state on every click.
BThe inline handler is cached automatically by React, so it never creates a new function on re-renders.
CUsing inline handlers disables React's event system, causing native DOM events to fire instead.
DThe inline handler creates a new function on every render, which can cause unnecessary re-renders in child components if passed as props.
Attempts:
2 left
💡 Hint

Think about how JavaScript functions behave inside JSX when the component re-renders.

📝 Syntax
intermediate
2:00remaining
Which React event handler syntax correctly binds a class method?

Given a React class component, which option correctly binds the handleClick method to the component instance?

React
class MyButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
    // Binding happens here
  }
  handleClick() {
    this.setState({ count: this.state.count + 1 });
  }
  render() {
    return <button onClick={this.handleClick}>Clicked {this.state.count} times</button>;
  }
}
Athis.handleClick = function() { this.setState({ count: this.state.count + 1 }); };
Bthis.handleClick = () => this.handleClick();
Cthis.handleClick = this.handleClick.bind(this);
DNo binding needed, just use onClick={this.handleClick}
Attempts:
2 left
💡 Hint

Remember how JavaScript class methods lose this context unless bound.

❓ state_output
advanced
2:00remaining
What is the output when using inline handlers with state updates in React functional components?

Examine this React functional component. What will be displayed after clicking the button twice?

React
function Counter() {
  const [count, setCount] = React.useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}
ACount: 2
BCount: NaN
CCount: 0
DCount: 1
Attempts:
2 left
💡 Hint

Consider how state updates work with the current state value inside the inline function.

🔧 Debug
advanced
2:00remaining
Why does this React component re-render infinitely with an inline handler?

Look at this React functional component. It causes an infinite re-render loop. Why?

React
function Timer() {
  const [seconds, setSeconds] = React.useState(0);
  React.useEffect(() => {
    const id = setInterval(() => setSeconds(seconds + 1), 1000);
    return () => clearInterval(id);
  }, [() => setSeconds(seconds + 1)]);
  return <div>Seconds: {seconds}</div>;
}
AThe setInterval callback is not updating state correctly, causing infinite updates.
BThe dependency array contains a new inline function on every render, causing useEffect to run repeatedly.
CThe clearInterval is missing, so intervals stack up infinitely.
DReact.useEffect cannot use arrow functions inside dependency arrays.
Attempts:
2 left
💡 Hint

Think about how React compares dependencies in useEffect.

🧠 Conceptual
expert
2:00remaining
Why prefer named function handlers over inline handlers in React components?

Which reason best explains why using named function handlers is often better than inline handlers in React?

ANamed handlers help React optimize rendering by keeping stable function references, reducing unnecessary child re-renders.
BInline handlers are deprecated in React and will be removed in future versions.
CInline handlers cause React to skip reconciliation, leading to UI inconsistencies.
DNamed handlers automatically batch state updates, while inline handlers do not.
Attempts:
2 left
💡 Hint

Think about how React uses props to decide when to re-render components.

Practice

(1/5)
1. What is the main difference between an inline event handler and a function handler in React?
easy
A. Inline handlers require binding, function handlers do not.
B. Inline handlers can only be used with class components, function handlers only with functional components.
C. Inline handlers are written directly inside JSX, while function handlers are separate functions passed by name.
D. Function handlers are faster because they run outside JSX, inline handlers cause syntax errors.

Solution

  1. Step 1: Understand inline handlers

    Inline handlers are written directly inside JSX tags, like <button onClick={() => alert('Hi')}>.
  2. Step 2: Understand function handlers

    Function handlers are separate functions defined outside JSX and passed by name, like onClick={handleClick}.
  3. Final Answer:

    Inline handlers are written directly inside JSX, while function handlers are separate functions passed by name. -> Option C
  4. Quick Check:

    Inline vs function handler difference = D [OK]
Hint: Inline = inside JSX, function = separate named function [OK]
Common Mistakes:
  • Confusing inline handlers with class component binding
  • Thinking inline handlers cause syntax errors
  • Believing function handlers are only for class components
2. Which of the following is the correct syntax for passing a function handler to a button's onClick event in React?
easy
A. Click me
B. Click me
C. Click me
D. handleClick}>Click me

Solution

  1. Step 1: Identify correct function handler syntax

    In React, you pass the function reference without calling it, so onClick={handleClick} is correct.
  2. Step 2: Check other options

    Click me calls the function immediately, Click me uses string which is invalid in JSX, handleClick}>Click me passes a function returning a function, which is not typical.
  3. Final Answer:

    <button onClick={handleClick}>Click me</button> -> Option A
  4. Quick Check:

    Pass function reference without parentheses = A [OK]
Hint: Pass function name without () for handler [OK]
Common Mistakes:
  • Calling the function immediately with parentheses
  • Using string quotes instead of curly braces
  • Wrapping handler in unnecessary arrow function
3. Consider this React code snippet:
function MyButton() {
  const handleClick = () => alert('Clicked!');
  return <button onClick={() => handleClick()}>Click me</button>;
}

What happens when the button is clicked?
medium
A. A syntax error occurs due to incorrect handler syntax.
B. An alert with 'Clicked!' appears.
C. Nothing happens because the handler is not called.
D. The alert appears twice.

Solution

  1. Step 1: Analyze the inline handler

    The button's onClick uses an inline arrow function that calls handleClick() when clicked.
  2. Step 2: Understand handleClick behavior

    handleClick shows an alert with 'Clicked!'. So clicking triggers the alert once.
  3. Final Answer:

    An alert with 'Clicked!' appears. -> Option B
  4. Quick Check:

    Inline calls function correctly = C [OK]
Hint: Inline arrow calls function, alert shows once [OK]
Common Mistakes:
  • Thinking the handler is not called
  • Assuming syntax error from arrow function
  • Believing alert appears multiple times
4. Identify the error in this React component:
function Counter() {
  const [count, setCount] = React.useState(0);
  return <button onClick={setCount(count + 1)}>Increment</button>;
}
medium
A. The onClick handler calls setCount immediately instead of on click.
B. The useState hook is used incorrectly.
C. The button element is missing a closing tag.
D. There is no error; the code works fine.

Solution

  1. Step 1: Check onClick handler usage

    The handler is written as onClick={setCount(count + 1)}, which calls setCount immediately during render.
  2. Step 2: Correct usage

    It should be a function: onClick={() => setCount(count + 1)} to call on click.
  3. Final Answer:

    The onClick handler calls setCount immediately instead of on click. -> Option A
  4. Quick Check:

    Handler must be a function, not immediate call = A [OK]
Hint: onClick needs a function, not a direct call [OK]
Common Mistakes:
  • Calling state setter immediately in JSX
  • Ignoring need for arrow function wrapper
  • Assuming useState is incorrect here
5. You want to optimize a React component that renders a list of buttons. Each button increments its own counter. Which approach is better and why?
Option 1: Inline handler
<button onClick={() => increment(index)}>Increment</button>

Option 2: Function handler
function handleClick() { increment(index); }
<button onClick={handleClick}>Increment</button>
hard
A. Option 1 is better because inline handlers are always faster.
B. Option 1 is better because function handlers cause memory leaks.
C. Both options are identical in performance and readability.
D. Option 2 is better because it avoids creating a new function on every render.

Solution

  1. Step 1: Understand inline handler cost

    Inline handlers create a new function every render, which can hurt performance in large lists.
  2. Step 2: Understand function handler benefit

    Function handlers defined outside JSX reuse the same function reference, improving performance and readability.
  3. Final Answer:

    Option 2 is better because it avoids creating a new function on every render. -> Option D
  4. Quick Check:

    Function handler reuse improves performance = B [OK]
Hint: Reuse handlers to avoid new functions each render [OK]
Common Mistakes:
  • Assuming inline handlers are always faster
  • Ignoring performance impact in lists
  • Believing function handlers cause memory leaks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard