0
0
Reactframework~5 mins

Handling events in React - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
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
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
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
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
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.