Skip to main content

Command Palette

Search for a command to run...

πŸ“˜ React Mastery Series β€” Day 22

Updated
β€’3 min readβ€’View as Markdown
πŸ“˜ React Mastery Series β€” Day 22
S

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 πŸ”₯

More from this blog