Hey little tech wizard! 🧙♂️
Let’s say you clicked a button, typed your name in a form, or submitted a message.
That’s called an event.
In React, we can catch those events and tell our app what to do next — like show a popup, send data to a server, or just say “Hello!” 👋
This article is all about how to handle events and build cool, interactive apps!
🎉 What Are Events in React?
Events are actions like:
- 🔘 Clicking a button
- ⌨️ Typing in an input
- 📨 Submitting a form
- 🖱️ Moving your mouse
React gives us a clean way to catch these events and respond to them.
Let’s start with a simple button click! 👇
✅ Basic Button Click Example
jsxCopyEditfunction ClickMe() {
function handleClick() {
alert("You clicked me! 🎉");
}
return <button onClick={handleClick}>Click Me</button>;
}
🔍 What’s Happening?
- We created a function
handleClick
. - We told the button to run it when clicked using
onClick
.
🧠 Note: React uses camelCase
(onClick
, not onclick
) and passes the function reference directly — no parentheses unless you want it to run immediately!
🔗 Binding Event Handlers in Class Components
Before Hooks, we had to bind this
in class components:
jsxCopyEditclass MyButton extends React.Component {
constructor() {
super();
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
alert("Class button clicked! 🎈");
}
render() {
return <button onClick={this.handleClick}>Click</button>;
}
}
But with arrow functions or functional components, we don’t need to bind anymore. Yay! 😄
🛑 Controlling Event Behavior
React events are synthetic, meaning they work the same in all browsers.
You can use two powerful tools to control event behavior:
1. event.preventDefault()
⛔
This stops the default action — like stopping a form from refreshing the page.
jsxCopyEditfunction MyForm() {
function handleSubmit(e) {
e.preventDefault(); // stops the default form submit
alert("Form submitted 🚀");
}
return (
<form onSubmit={handleSubmit}>
<input placeholder="Type something" />
<button type="submit">Submit</button>
</form>
);
}
2. event.stopPropagation()
🔇
This stops the event from bubbling up to parent elements.
jsxCopyEditfunction Parent() {
function parentClick() {
alert("Parent clicked 🏠");
}
function childClick(e) {
e.stopPropagation(); // stops event from reaching parent
alert("Child clicked 👶");
}
return (
<div onClick={parentClick}>
<button onClick={childClick}>Click Me</button>
</div>
);
}
🧪 React Forms – Controlled vs Uncontrolled
Let’s now talk about forms — inputs, checkboxes, and all that user magic ✨
🕹 Controlled Components
React controls the input using state.
jsxCopyEditfunction ControlledInput() {
const [text, setText] = React.useState("");
return (
<input
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="Type here..."
/>
);
}
💡 Controlled means:
- The input’s value is stored in React state.
- You can control it, validate it, and reset it easily.
🎛 Uncontrolled Components
React doesn’t manage the input — the DOM does.
jsxCopyEditfunction UncontrolledInput() {
const inputRef = React.useRef();
function handleSubmit() {
alert("Value: " + inputRef.current.value);
}
return (
<>
<input ref={inputRef} placeholder="Type here..." />
<button onClick={handleSubmit}>Submit</button>
</>
);
}
💡 Use uncontrolled inputs when:
- You don’t need to control or validate input live.
- You want simplicity or are integrating legacy code.
🔁 Event Types in React
Event Type | Description | Example Prop |
---|---|---|
Mouse Events | Click, hover, move mouse | onClick , onMouseEnter |
Keyboard Events | Key press, release | onKeyDown , onKeyUp |
Form Events | Input change, submit | onChange , onSubmit |
Focus Events | Input gets or loses focus | onFocus , onBlur |
🧠 Infographic: React Event Flow

A diagram showing: User Click → Synthetic Event → Your Handler → Optional preventDefault or stopPropagation
🧰 Pro Tips
🛠 Use controlled components for better form handling.
🚫 Always call event.preventDefault()
on form submissions.
🔁 Don’t forget to update state on input change using onChange
.
📚 Summary – Event Handling in React
Key Concept | What It Means |
---|---|
Event | An action like click or input |
onClick, onChange | React’s way to catch events |
event.preventDefault() | Stops default behavior like page reload |
Controlled Component | React manages input value with state |
Uncontrolled Component | DOM manages input using refs |
🙌 Let’s Build Real Apps Together!
Now you know how to make your React app interactive — just like a game controller! 🎮
Want to keep learning how to build smart and modern web apps?
👉 Subscribe & Join Web Codder:
- 📺 YouTube: Click to Subscribe
- 📸 Instagram: Follow Us
- 💬 WhatsApp Community: Join Here
Let’s learn React like a pro — the easy and fun way! 🔥