How do you attach a click event handler to a button in React?
Use the onClick attribute with a function, like <button onClick={handleClick}>Click me</button>. React uses camelCase for event names.
Click to reveal answer
intermediate
Why do we often use arrow functions or bind methods when passing event handlers in React?
Because event handlers lose the correct this context if not bound. Arrow functions keep this from the surrounding scope, ensuring the handler works as expected.
Click to reveal answer
beginner
What is the difference between onClick={handleClick()} and onClick={handleClick} in React?
onClick={handleClick} passes the function to be called on click. onClick={handleClick()} calls the function immediately during render, which is usually wrong.
Click to reveal answer
beginner
How can you access the event object in a React event handler?
React passes the event object as the first argument to the handler function, e.g., <code>function handleClick(event) { /* use event here */ }</code>.
Click to reveal answer
intermediate
What is synthetic event in React?
React wraps native browser events in a synthetic event to provide consistent behavior across browsers. It has the same interface as native events but works the same everywhere.
Click to reveal answer
Which attribute is used to handle a click event in React?
AonClick
Bonclick
CclickHandler
DhandleClick
✗ Incorrect
React uses camelCase event names like onClick.
What happens if you write onClick={handleClick()} instead of onClick={handleClick}?
AIt causes a syntax error
BThe function runs immediately during render
CThe function runs only when clicked
DNothing happens
✗ Incorrect
Using parentheses calls the function immediately instead of passing it as a handler.
How do you access the event object in a React event handler?
AIt is passed as the first argument to the handler function
BYou must import it separately
CIt is a global variable
DReact does not provide an event object
✗ Incorrect
React passes the event object as the first argument to event handlers.
Why use arrow functions for event handlers in React?
ATo avoid syntax errors
BTo make the code run faster
CTo keep the correct this context
DArrow functions are required by React
✗ Incorrect
Arrow functions keep the surrounding this context, avoiding bugs.
What is a synthetic event in React?
AA fake event that does nothing
BA deprecated React feature
CAn event only used in testing
DA wrapper around native events for cross-browser consistency
✗ Incorrect
Synthetic events wrap native events to behave the same across browsers.
Explain how to add a click event handler to a button in React and why binding or arrow functions are important.
Think about how JavaScript functions handle 'this' inside React components.
You got /4 concepts.
Describe what a synthetic event is in React and why it is useful.
Consider how different browsers handle events differently.
You got /4 concepts.
Practice
(1/5)
1. In React, how do you correctly attach a click event handler to a button?
easy
A. <button onclick="handleClick()">Click me</button>
B. <button onClick={handleClick}>Click me</button>
C. <button OnClick={handleClick}>Click me</button>
D. <button onClick="handleClick()">Click me</button>
Solution
Step 1: Understand React event naming
React uses camelCase for event handlers, so onClick is correct, not lowercase or capitalized differently.
Step 2: Attach event handler as a function reference
In React, event handlers are passed as functions, not strings. So {handleClick} is correct, not a string like "handleClick()".
Final Answer:
<button onClick={handleClick}>Click me</button> -> Option B
C. Incorrect event name 'onclick' should be 'onClick'
D. handleClick function is not defined
Solution
Step 1: Check event handler attribute name
React requires camelCase event names, so onclick is incorrect; it should be onClick.
Step 2: Verify function definition and usage
The function handleClick is correctly defined and passed as a reference, so no error there.
Final Answer:
Incorrect event name 'onclick' should be 'onClick' -> Option C
Quick Check:
React event names are camelCase = Incorrect event name 'onclick' should be 'onClick' [OK]
Hint: React event names use camelCase, not lowercase [OK]
Common Mistakes:
Using lowercase event names
Forgetting to pass function reference
Misunderstanding JSX syntax for elements
5. You want to create a React component that increments a counter only when the user presses the Enter key inside an input box. Which is the correct way to handle this event?
function EnterCounter() {
const [count, setCount] = React.useState(0);
const handleKeyDown = (event) => {
// What condition should be here?
};
return (
<>
<input type="text" onKeyDown={handleKeyDown} aria-label="Enter key counter input" />
<p>Count: {count}</p>
</>
);
}
hard
A. if (event.which === 'Enter') setCount(count + 1);
B. if (event.keyCode === 13) setCount(count + 1);
C. if (event.key === 13) setCount(count + 1);
D. if (event.key === 'Enter') setCount(count + 1);
Solution
Step 1: Identify the correct property for key detection
Modern React and browsers use event.key as a string to detect keys, so checking event.key === 'Enter' is correct.
Step 2: Understand deprecated properties
event.keyCode and event.which are deprecated or inconsistent; also, comparing event.key to a number like 13 is incorrect.
Final Answer:
if (event.key === 'Enter') setCount(count + 1); -> Option D
Quick Check:
Use event.key string for key detection = if (event.key === 'Enter') setCount(count + 1); [OK]
Hint: Use event.key === 'Enter' to detect Enter key presses [OK]
Common Mistakes:
Using deprecated event.keyCode or event.which
Comparing event.key to a number instead of string
Not checking key inside keydown event
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.