📖 Scenario: You are building a simple React button component that counts how many times it is clicked. You want to learn the difference between using inline event handlers and function event handlers.
🎯 Goal: Create a React component that shows a button and a count. First, set up the initial state. Then, add a configuration variable for the increment step. Next, implement the click handler using a function. Finally, add a second button that uses an inline click handler to increase the count.
📋 What You'll Learn
Use React functional components with hooks
Create a state variable called count initialized to 0
Create a constant step set to 1
Create a function handler called handleClick that increases count by step
Add a button that uses handleClick as its onClick handler
Add a second button that uses an inline arrow function to increase count by step
💡 Why This Matters
🌍 Real World
Buttons with click handlers are everywhere in web apps. Knowing how to write handlers inline or as functions helps you write cleaner and more efficient React code.
💼 Career
React developers often need to manage user interactions. Understanding event handlers is essential for building interactive user interfaces.
Progress0 / 4 steps
1
Set up the initial state
Create a React functional component called Counter. Inside it, use the useState hook to create a state variable called count initialized to 0.
React
Hint
Use const [count, setCount] = useState(0); inside the Counter function.
2
Add a configuration variable
Inside the Counter component, create a constant called step and set it to 1.
React
Hint
Just add const step = 1; inside the Counter function.
3
Create a function click handler
Inside the Counter component, create a function called handleClick that increases count by step using setCount. Then, add a button with the text Increment that uses handleClick as its onClick handler. Also, display the current count in a <p> tag.
React
Hint
Define handleClick as a function that calls setCount(count + step). Return JSX with a <p> showing count and a button using onClick={handleClick}.
4
Add a button with inline click handler
Inside the Counter component's return statement, add a second button with the text Inline Increment. Use an inline arrow function for its onClick handler that increases count by step using setCount.
React
Hint
Add a button with onClick={() => setCount(count + step)} and text Inline Increment.
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
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, like onClick={handleClick}.
Final Answer:
Inline handlers are written directly inside JSX, while function handlers are separate functions passed by name. -> Option C
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
Step 1: Identify correct function handler syntax
In React, you pass the function reference without calling it, so onClick={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 A
Quick Check:
Pass function reference without parentheses = A [OK]
Hint: Pass function name without () for handler [OK]