REACT / LESSON 05
Event Handling.
Learn how to respond to user interactions like clicks, typing, and form submissions in React

Loading the lesson…
Loading activities…
REACT / LESSON 05
Learn how to respond to user interactions like clicks, typing, and form submissions in React

Loading the lesson…
Loading activities…
Every interactive app has to respond to what people do. They click buttons, type in fields, submit forms and hover over stuff. In React you handle all of that with event handlers, which are just functions that run when something happens.
In React you attach event handlers right onto JSX elements using camelCase props like onClick, onChange and onSubmit.
Look closely here. You pass the function itself (handleClick), and you do NOT pass a function call (handleClick()). If you wrote onClick={handleClick()}, the function would fire the moment the component renders instead of waiting for somebody to click.
For short handlers, you can write an arrow function right there in the JSX.
Inline handlers are great for simple one-liners. Once the logic gets longer, pull it out into a named function so future you can actually read it.
React supports all the standard browser events. These are the ones you'll use the most.
| Event | Triggers When | Common Uses |
|---|---|---|
onClick | User clicks an element | Buttons, links, cards |
onChange | Input value changes | Text inputs, checkboxes, selects |
onSubmit | Form is submitted | Form handling |
onKeyDown | Key is pressed | Keyboard shortcuts, Enter to submit |
onMouseEnter | Cursor enters an element | Tooltips, hover effects |
onMouseLeave | Cursor leaves an element | Hiding hover effects |
onFocus | Element gains focus | Showing helper text |
onBlur | Element loses focus | Validation on field exit |
Every event handler gets handed an event object with the details of what just happened.
A few properties on the event object you'll reach for constantly.
event.target - the DOM element that triggered the eventevent.target.value - the current value of an inputevent.key - which keyboard key got pressedevent.preventDefault() - stops the browser's default actionTo keep track of what somebody types, pair onChange with state.
That's called a controlled input, because React controls the input's value through state. Every keystroke updates state, which re-renders the component, which updates the value on screen. Round and round it goes.
Browsers have built-in behaviors for certain events. Forms reload the page when you submit them, and links navigate you away. Use event.preventDefault() to stop those defaults in their tracks.
Sometimes your handler needs a bit of extra info. The move is to wrap the handler in an arrow function.
The arrow function () => handleSelect(fruit) creates a brand new function for each item, and each one remembers which fruit to pass along.
When a form has a bunch of fields, you can get away with one handler by using the input's name attribute.
The computed property [name]: value sets the right field based on whichever input fired the event. One handler, all three fields handled.
onClick, onChange, onSubmit)handleClick), and don't call it (handleClick())event.target.value and event.keyevent.preventDefault() to stop default browser behaviors (like a form reloading the page)value from state with onChange to track what people typeYou know how to handle events inside a single component now. But what happens when a child component needs to tell its parent that something happened? Next lesson is Props and Data Flow, all about passing data down and sending events back up the component tree...