Lesson 17/100

Tutorials React.js Tutorial

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

BeginnerIntermediateAdvancedProfessional

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

CodeWhat 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

  1. Add onClick to a button.
  2. Add onChange to an input and log e.target.value.
  3. Use onSubmit on a form with preventDefault.
  4. Read the real-world section and name which part of the app uses this topic.
  5. Run the example locally and confirm the same behavior in the browser.
  6. 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.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
React Developer Tools:?
Short answer: Use the Profiler tab to measure the render times and identify slow components. Check for unnecessary re-renders and optimize with React.memo or shouldComponentUpdate. Real-world example (ShopNest) ShopNest’…
Mid PDF Detailed
Mock axios:?
Short answer: import axios from 'axios'; jest.mock('axios'); axios.get.mockResolvedValue({ data: 'mock data' }); Example code import axios from 'axios'; jest.mock('axios'); axios.get.mockResolvedValue({ data: 'mock data'…
Mid PDF Detailed
Choose a Testing Library: Use libraries like Jest (test runner) and React Testing?
Short answer: Library (for testing React components). Real-world example (ShopNest) ShopNest’s storefront is React: components for ProductCard, CartDrawer, and CheckoutForm, with hooks for local UI state. Say this in the…
Mid PDF Detailed
Only call hooks at the top level?
Short answer: (Don’t call hooks inside loops, conditions, or nested functions.) Real-world example (ShopNest) ShopNest’s cart icon uses useState for count and useEffect to load the cart once on mount. Say this in the int…
Mid PDF Detailed
Mock axios: import axios from 'axios'; jest.mock('axios');?
Short answer: xios.get.mockResolvedValue({ data: 'mock data' }); xios.get.mockResolvedValue({ data: 'mock data' }); xios.get.mockResolvedValue({ data: 'mock data' }); xios.get.mockResolvedValue({ data: 'mock data' }); Re…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

React.js Tutorial
Course syllabus

React.js Tutorial

Module 1: React Basics & Setup
Module 2: Props, Events & Lists
Module 3: Forms & Hooks
Module 4: Routing & Data
Module 5: State & Authentication
Module 6: Architecture & React 19
Module 7: Performance
Module 8: Full-Stack & Real-Time
Module 9: Testing & Deployment
Module 10: ShopCart Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details