0
0
Reactframework~20 mins

Inline vs function handlers in React - Practice Questions

Choose your learning style9 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.