Handling Events
Events are how users interact with your application. React provides a consistent, cross-browser event system called Synthetic Events.React Events vs DOM Events
| Feature | HTML/DOM | React |
|---|---|---|
| Naming | lowercase (onclick) | camelCase (onClick) |
| Handler | String ("handleClick()") | Function ({handleClick}) |
| Prevent default | return false; | e.preventDefault() |
| Event object | Browser-specific | Normalized (Synthetic) |
Basic Event Handling
Defining Event Handlers
Don’t Call the Function!
The Event Object
React wraps native browser events in a SyntheticEvent for cross-browser compatibility.Common Event Properties
| Property | Description |
|---|---|
event.target | Element that triggered the event |
event.currentTarget | Element the handler is attached to |
event.type | Event type ('click', 'change', etc.) |
event.preventDefault() | Prevents default browser behavior |
event.stopPropagation() | Stops event bubbling |
event.nativeEvent | Underlying native browser event |
Passing Arguments to Event Handlers
Often you need to pass data (like an item ID) to the handler.Accessing Event AND Custom Arguments
Mouse Events
Mouse Event Types
| Event | Triggers When |
|---|---|
onClick | Mouse button clicked |
onDoubleClick | Mouse button double-clicked |
onMouseEnter | Cursor enters element (doesn’t bubble) |
onMouseLeave | Cursor leaves element (doesn’t bubble) |
onMouseOver | Cursor over element (bubbles) |
onMouseOut | Cursor leaves element (bubbles) |
onMouseMove | Cursor moves over element |
onMouseDown | Mouse button pressed |
onMouseUp | Mouse button released |
onContextMenu | Right-click (context menu) |
Keyboard Events
Key Event Properties
| Property | Description |
|---|---|
event.key | The key value ('Enter', 'a', 'ArrowUp') |
event.code | Physical key code ('KeyA', 'Enter') |
event.altKey | Alt key held |
event.ctrlKey | Ctrl key held |
event.shiftKey | Shift key held |
event.metaKey | Meta key held (Cmd on Mac) |
Building Keyboard Shortcuts
Form Events
Input Change Event
Focus Events
Event Propagation
Events bubble up through the DOM tree. You can stop this behavior.Event Capturing
React also supports the capture phase (rarely needed):Touch Events (Mobile)
Drag and Drop Events
🎯 Practice Exercises
Exercise 1: Like Button with Double-Click to Super Like
Exercise 1: Like Button with Double-Click to Super Like
Exercise 2: Keyboard-Controlled Character
Exercise 2: Keyboard-Controlled Character
Exercise 3: Click Outside to Close
Exercise 3: Click Outside to Close
Summary
| Concept | Description |
|---|---|
| Event Handlers | Functions that respond to user interactions |
| SyntheticEvent | React’s cross-browser event wrapper |
| event.target | Element that triggered the event |
| event.preventDefault() | Stop default browser behavior |
| event.stopPropagation() | Stop event bubbling |
| Mouse Events | click, doubleClick, mouseEnter, mouseLeave, etc. |
| Keyboard Events | keyDown, keyUp, keyPress (use key property) |
| Form Events | onChange, onSubmit, onFocus, onBlur |
| Touch Events | touchStart, touchMove, touchEnd (mobile) |
Next Steps
In the next chapter, you’ll learn about Lists & Keys — efficiently rendering collections of data!