Hackforge Academy

Category: react

React, event handling

Published on 04 May 2026

Explanation

In React, event handling refers to how user interactions like clicks, typing, or form submissions are managed. React uses a synthetic event system that works consistently across all browsers.

Code:

function App() {
  function handleClick() {
    console.log("Button clicked");
  }

  return <button onClick={handleClick}>
Click Me
</button>;
}

Explanation

Event handlers in React are written in camelCase (e.g., onClick, onChange) and are passed as functions instead of strings.

Code:

<button onClick={() => 
console.log('Clicked')}>Click</button>

Explanation

You can pass parameters to event handlers using arrow functions.

Code:

function App() {
  function greet(name) {
    console.log("Hello " + name);
  }

  return <button onClick={() => 
greet('hackforge')}>hackforge Academy
</button>;
}

Explanation

Handling form events like input change using onChange and managing state.

Code:

import { useState } from 'react';

function App() {
  const [name, setName] = useState('');

  return (
    <input
      type="text"
      value={name}
      onChange={(e) => 
setName(e.target.value)}
    />
  );
}

Explanation

Preventing default behavior using event.preventDefault(), commonly used in form submissions.

Code:

function App() {
  function handleSubmit(e) {
    e.preventDefault();
    console.log("Form submitted");
  }

  return (
    <form onSubmit={handleSubmit}>
      <button type="submit">Submit</button>
    </form>
  );
}

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community