π React Mastery Series β Day 22

Frontend Developer | React & JavaScript | Building production-ready web apps Developer with 1.5+ years of experience, currently focused on React, Redux, and full-stack development. Sharing practical tutorials, concepts, and project learnings.
Handling Events in React β Clean & Scalable Event Logic
User interaction is the heart of every application.
Clicking buttons
Submitting forms
Typing in inputs
Hover effects
Keyboard actions
All of this works using Event Handling in React.
π€ What is Event Handling?
Event Handling in React allows us to respond to user actions like clicks, changes, submissions, etc.
React events are very similar to JavaScript events β but written in camelCase.
π§ Basic Example β Button Click
function ClickExample() {
const handleClick = () => {
alert("Button Clicked!");
};
return <button onClick={handleClick}>Click Me</button>;
}
β onClick is camelCase
β We pass function reference (not function call)
Correct:
onClick={handleClick}
Wrong:
onClick={handleClick()}
π§ Passing Arguments to Events
Sometimes we need to send data.
function UserGreeting() {
const greetUser = (name) => {
alert(`Hello ${name}`);
};
return (
<button onClick={() => greetUser("Shailaja")}>
Greet
</button>
);
}
We use arrow function to pass arguments.
π§ Handling Input Change
Very common in forms.
import { useState } from "react";
function InputExample() {
const [value, setValue] = useState("");
const handleChange = (e) => {
setValue(e.target.value);
};
return (
<input
type="text"
value={value}
onChange={handleChange}
/>
);
}
β e.target.value gives input value
β Used in Controlled Components
π§ Form Submission Event
function FormExample() {
const handleSubmit = (e) => {
e.preventDefault();
alert("Form Submitted!");
};
return (
<form onSubmit={handleSubmit}>
<button type="submit">Submit</button>
</form>
);
}
Important:
π e.preventDefault() prevents page reload.
π§ Common React Events
Event | Usage |
|---|---|
onClick | Button click |
onChange | Input change |
onSubmit | Form submit |
onMouseEnter | Hover |
onKeyDown | Keyboard press |
π― Best Practices
β Keep event logic separate from JSX
β Avoid writing complex logic inline
β Use meaningful function names
β Reuse handlers when possible
Clean example:
const handleLogin = () => {
// login logic
};
<button onClick={handleLogin}>Login</button>
π§ Interview One-Liner
React handles events using camelCase event handlers and function references to respond to user interactions efficiently.
π How It Connects
β Forms (Day 18)
β Conditional Rendering (Day 20)
β Lists & Keys (Day 21)
β Lifting State (Day 19)
Events are how users interact with your state.
β Key Takeaways
β React uses camelCase event names
β Always pass function reference
β Use preventDefault for forms
β Keep logic clean and organized
π Coming Next
Day 23 β Refs Forwarding & Advanced useRef Patterns
Now we enter slightly advanced territory π₯




