JavaScript Curriculum
Event Handling
easyThe coffee shop menu needs clickable Order buttons, a search input that filters live, and a contact form that validates before submitting. Every interaction is driven by event handlers wired to state.
Event Handling
React uses synthetic events — cross-browser wrappers around native DOM events. You attach handlers as JSX props (onClick, onChange, onSubmit) and receive a SyntheticEvent object with the same API as native events.
Event Handler Patterns
Four ways to write event handlers — each with the right use case:
Handler defined before the return. Keeps JSX clean and makes the handler reusable, testable, and debuggable.
function OrderButton({ item, onOrder }) { const handleClick = () => { console.log('Ordering:', item.name) onOrder(item) } return ( <button onClick={handleClick}> Order {item.name} </button> ) }
// Click a button to see events
SyntheticEvent — What You Get
Every event handler receives a SyntheticEvent. Explore what each event type gives you:
e.type"click"e.target<button>e.currentTarget<div>e.clientX/Y{ x: 340, y: 210 }e.button0Controlled Form with Live State
A fully controlled form — every field wired to state, one handler for all inputs:
{
"name": "",
"email": "",
"category": "coffee",
"newsletter": false,
"message": ""
}handleChange handles all inputs — reads name attr to know which field to update.Your Challenge
Build a live search: const [search, setSearch] = useState(''). Wire onChange to update it. Derive filtered = items.filter(i => i.name.toLowerCase().includes(search.toLowerCase())). Add onKeyDown: if e.key === 'Escape' clear the search. Show the count of visible items.
Challenge
Build a controlled search input that filters a list of menu items as you type. Wire onChange to update a searchTerm state. Derive filteredItems from the state. Add a keyboard handler that clears the input on Escape.
Event Handling
easyThe coffee shop menu needs clickable Order buttons, a search input that filters live, and a contact form that validates before submitting. Every interaction is driven by event handlers wired to state.
Event Handling
React uses synthetic events — cross-browser wrappers around native DOM events. You attach handlers as JSX props (onClick, onChange, onSubmit) and receive a SyntheticEvent object with the same API as native events.
Event Handler Patterns
Four ways to write event handlers — each with the right use case:
Handler defined before the return. Keeps JSX clean and makes the handler reusable, testable, and debuggable.
function OrderButton({ item, onOrder }) { const handleClick = () => { console.log('Ordering:', item.name) onOrder(item) } return ( <button onClick={handleClick}> Order {item.name} </button> ) }
// Click a button to see events
SyntheticEvent — What You Get
Every event handler receives a SyntheticEvent. Explore what each event type gives you:
e.type"click"e.target<button>e.currentTarget<div>e.clientX/Y{ x: 340, y: 210 }e.button0Controlled Form with Live State
A fully controlled form — every field wired to state, one handler for all inputs:
{
"name": "",
"email": "",
"category": "coffee",
"newsletter": false,
"message": ""
}handleChange handles all inputs — reads name attr to know which field to update.Your Challenge
Build a live search: const [search, setSearch] = useState(''). Wire onChange to update it. Derive filtered = items.filter(i => i.name.toLowerCase().includes(search.toLowerCase())). Add onKeyDown: if e.key === 'Escape' clear the search. Show the count of visible items.
Challenge
Build a controlled search input that filters a list of menu items as you type. Wire onChange to update a searchTerm state. Derive filteredItems from the state. Add a keyboard handler that clears the input on Escape.