React CSS Styling — Complete Guide
React CSS Styling — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.
On this page
React.js Tutorial · Lesson 17 of 100
Event Handling
Beginner → Intermediate → Advanced → Professional
Beginner · 1 — Foundations · ~12 min read · Module 2: JavaScript & React Fundamentals
Introduction
This lesson is part of the beginner section. We explain Event Handling slowly, with examples you can copy and run. If something is unclear, read it twice — that is how everyone learns. Event handling in React uses camelCase props like onClick, onChange, and onSubmit. You pass a function — React calls it when the user acts. Buttons, forms, and inputs need handlers. React wraps browser events so they work the same across browsers.
Event Handling appears in almost every file you will write. Once it clicks, later lessons (hooks, routing, APIs) become much easier.
When will you use this?
You use this in every component you write from your first app to your tenth year on the job.
- Product pages, login forms, and todo lists all use components, props, and state.
- When a user types in a search box, React state holds the text and filters a list.
Real-world: HDFC quick pay form
HDFC quick transfer form submits without full page reload. onSubmit handler validates amount, calls transfer API, and shows success toast — preventDefault stops browser navigation.
Production-style code
function QuickPayForm({ onTransfer }) {
const [amount, setAmount] = useState('');
const [toAccount, setToAccount] = useState('');
function handleSubmit(e) {
e.preventDefault();
if (Number(amount) <= 0) return alert('Enter valid amount');
onTransfer({ toAccount, amount: Number(amount) });
}
return (
<form onSubmit={handleSubmit}>
<input value={toAccount} onChange={e => setToAccount(e.target.value)} placeholder="Account" />
<input value={amount} onChange={e => setAmount(e.target.value)} placeholder="Amount" />
<button type="submit">Pay Now</button>
</form>
);
}
What happens in production: Every banking and checkout flow uses the same onSubmit + preventDefault pattern — users stay on the SPA while money moves.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
function LoginForm() {
function handleSubmit(e) {
e.preventDefault();
alert('Form submitted');
}
return (
<form onSubmit={handleSubmit}>
<button type="submit">Login</button>
</form>
);
}
Line-by-line walkthrough
| Code | What it means |
|---|---|
function LoginForm() { | Defines a function — often a component or event handler. |
function handleSubmit(e) { | Defines a function — often a component or event handler. |
e.preventDefault(); | Part of the Event Handling example — read it together with the lines before and after. |
alert('Form submitted'); | Part of the Event Handling example — read it together with the lines before and after. |
} | Closes a block started by { or ( above. |
return ( | Sends UI or a value back to whoever called this function. |
<form onSubmit={handleSubmit}> | JSX tag — a UI element or custom component on the page. |
<button type="submit">Login</button> | JSX tag — a UI element or custom component on the page. |
</form> | Part of the Event Handling example — read it together with the lines before and after. |
); | Closes a block started by { or ( above. |
} | Closes a block started by { or ( above. |
How it works (big picture)
- e.preventDefault() stops the browser from reloading the page on form submit.
- Pass handleSubmit without () — onClick={handleSubmit} not onClick={handleSubmit()}.
Do this on your computer
- Add onClick to a button.
- Add onChange to an input and log e.target.value.
- Use onSubmit on a form with preventDefault.
- Read the real-world section and name which part of the app uses this topic.
- Run the example locally and confirm the same behavior in the browser.
- Change one value in the example (text, initial state, or URL) and predict what will happen before you save.
Experiments — try changing this
- Change text or labels in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error message, then fix it.
Remember
Use onClick, onChange, onSubmit with camelCase. Pass a function reference, not a function call (usually). preventDefault on forms when handling submit in JS.
Common questions
Inline arrow vs named function?
Both work; named functions are easier to read and test.
How long should I spend on Event Handling?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.
What if I get stuck on Event Handling?
Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is Event Handling used in real jobs?
See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.