events

what is React Events ?

  • Profile picture of Mcs
  • by Mcs July 3, 2025

In React, events work just like in HTML/JavaScript — but with a few differences to make them more powerful and consistent across browsers.

Basic Syntax

React uses camelCase for event names and passes a function instead of a string:

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

Key Differences from HTML

HTMLReact
<button onclick="fn()"><button onClick={fn}>
String as handlerFunction reference
Lowercase (onclick)CamelCase (onClick)
Uses native eventUses SyntheticEvent

Common React Events

Event TypeReact EventExample
MouseonClick<button onClick={fn}>
FormonChange<input onChange={fn}>
FormonSubmit<form onSubmit={fn}>
KeyboardonKeyDown<input onKeyDown={fn}>
KeyboardonKeyUp<input onKeyUp={fn}>
Focus/BluronFocus, onBlur<input onFocus={fn}>
ClipboardonCopy, onPaste<input onPaste={fn}>
TouchonTouchStart<div onTouchStart={fn}>

Example: Button Click

function App() {
 function handleClick() {
   alert('Button clicked!');
 }

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

Example: Form Submit

function Form() {
 function handleSubmit(e) {
   e.preventDefault(); // Prevent page reload
   console.log('Form submitted');
 }

 return (
   <form onSubmit={handleSubmit}>
     <input type="text" placeholder="Your name" />
     <button type="submit">Submit</button>
   </form>
 );
}

Example: Input Change

function InputBox() {
 const [value, setValue] = useState('');

 return (
   <>
     <input
       type="text"
       value={value}
       onChange={(e) => setValue(e.target.value)}
     />
     <p>You typed: {value}</p>
   </>
 );
}

Summary

ConceptDescription
onClick, onChangeMost common React events
SyntheticEventReact’s cross-browser event system
e.preventDefault()Prevent default browser behavior (like form submit)
Event handlerShould be a function (not a string)

Comments

Add new comment

Restricted HTML

  • Allowed HTML tags: <br> <p> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <cite> <dl> <dt> <dd> <a hreflang href> <blockquote cite> <ul type> <ol type start> <strong> <em> <code> <li>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.