Event Handling
How to respond to user interactions like clicks, inputs, and form submissions.
What it is
React allows you to respond to user interactions by attaching **event handlers** directly to JSX elements.
Instead of lowercase HTML attributes like onclick, React uses camelCase props like onClick, onChange, and onSubmit.
Why it matters
Without event handlers, your UI is completely static. Event handlers are the bridge between the user's actions and your component's logic.
React normalizes these events across all browsers, so an onClick behaves exactly the same in Chrome, Firefox, and Safari without you having to write browser-specific workarounds.
How to use it
You define a function inside your component, usually starting with the word handle (e.g., handleClick or handleSubmit).
You then pass that function as a prop to a JSX element: <button onClick={handleClick}>.
The Click Trap
Test the buttons below. The first button passes a function call (with parentheses). The second button passes a function reference.
Notice how the broken function executed the moment the component appeared on screen. The fixed buttons wait patiently for your click.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- Event names are camelCase in React (
onClick,onChange). - Pass a function reference, not a function call (no parentheses).
- If you need to pass arguments, wrap the call in an arrow function:
onClick={() => deleteItem(id)}. - Call
e.preventDefault()for forms and links that shouldn't trigger browser navigations.
Done with this concept?
Mark it complete to track your progress. No login needed.