We use event handlers to respond when users click buttons or do other actions. Inline handlers are written directly inside the element, while function handlers are separate functions. Choosing between them helps keep code clear and efficient.
Inline vs function handlers in React
/* Inline handler example */ <button onClick={() => alert('Clicked!')}>Click me</button> /* Function handler example */ function handleClick() { alert('Clicked!'); } <button onClick={handleClick}>Click me</button>
Inline handlers use arrow functions directly inside JSX.
Function handlers are defined separately and passed by name.
<button onClick={() => alert('Hello!')}>Say Hello</button>function greet() { alert('Hello!'); } <button onClick={greet}>Say Hello</button>
function greet(name) { alert(`Hello, ${name}!`); } <button onClick={() => greet('Alice')}>Greet Alice</button>
const handleClick = () => { console.log('Button clicked'); }; <button onClick={handleClick}>Log Click</button>
This React component shows two buttons that both add 1 to a counter. One uses an inline arrow function, the other uses a separate function. Both update the count state and show the new value.
import React, { useState } from 'react'; export default function InlineVsFunction() { const [count, setCount] = useState(0); // Function handler increments count function increment() { setCount(prevCount => prevCount + 1); } return ( <main> <h1>Count: {count}</h1> {/* Inline handler increments count by 1 */} <button onClick={() => setCount(prevCount => prevCount + 1)}>Inline +1</button> {/* Function handler increments count by 1 */} <button onClick={increment}>Function +1</button> </main> ); }
Inline handlers create a new function every render, which can affect performance if overused.
Function handlers help keep code organized and can be reused easily.
Use inline handlers for simple, quick actions; use function handlers for complex or repeated logic.
Inline handlers are quick and written inside JSX.
Function handlers are separate functions passed by name.
Choosing the right one helps keep your React code clean and efficient.
Practice
Solution
Step 1: Understand inline handlers
Inline handlers are written directly inside JSX tags, like<button onClick={() => alert('Hi')}>.Step 2: Understand function handlers
Function handlers are separate functions defined outside JSX and passed by name, likeonClick={handleClick}.Final Answer:
Inline handlers are written directly inside JSX, while function handlers are separate functions passed by name. -> Option CQuick Check:
Inline vs function handler difference = D [OK]
- Confusing inline handlers with class component binding
- Thinking inline handlers cause syntax errors
- Believing function handlers are only for class components
Solution
Step 1: Identify correct function handler syntax
In React, you pass the function reference without calling it, soonClick={handleClick}is correct.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.Final Answer:
<button onClick={handleClick}>Click me</button> -> Option AQuick Check:
Pass function reference without parentheses = A [OK]
- Calling the function immediately with parentheses
- Using string quotes instead of curly braces
- Wrapping handler in unnecessary arrow function
function MyButton() {
const handleClick = () => alert('Clicked!');
return <button onClick={() => handleClick()}>Click me</button>;
}What happens when the button is clicked?
Solution
Step 1: Analyze the inline handler
The button's onClick uses an inline arrow function that callshandleClick()when clicked.Step 2: Understand handleClick behavior
handleClickshows an alert with 'Clicked!'. So clicking triggers the alert once.Final Answer:
An alert with 'Clicked!' appears. -> Option BQuick Check:
Inline calls function correctly = C [OK]
- Thinking the handler is not called
- Assuming syntax error from arrow function
- Believing alert appears multiple times
function Counter() {
const [count, setCount] = React.useState(0);
return <button onClick={setCount(count + 1)}>Increment</button>;
}Solution
Step 1: Check onClick handler usage
The handler is written asonClick={setCount(count + 1)}, which calls setCount immediately during render.Step 2: Correct usage
It should be a function:onClick={() => setCount(count + 1)}to call on click.Final Answer:
The onClick handler calls setCount immediately instead of on click. -> Option AQuick Check:
Handler must be a function, not immediate call = A [OK]
- Calling state setter immediately in JSX
- Ignoring need for arrow function wrapper
- Assuming useState is incorrect here
Option 1: Inline handler
<button onClick={() => increment(index)}>Increment</button>
Option 2: Function handler
function handleClick() { increment(index); }
<button onClick={handleClick}>Increment</button>
Solution
Step 1: Understand inline handler cost
Inline handlers create a new function every render, which can hurt performance in large lists.Step 2: Understand function handler benefit
Function handlers defined outside JSX reuse the same function reference, improving performance and readability.Final Answer:
Option 2 is better because it avoids creating a new function on every render. -> Option DQuick Check:
Function handler reuse improves performance = B [OK]
- Assuming inline handlers are always faster
- Ignoring performance impact in lists
- Believing function handlers cause memory leaks
Ready to prove it?
Pass the test and this pattern moves unknown
