what is React Events ?
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
| HTML | React |
|---|---|
| <button onclick="fn()"> | <button onClick={fn}> |
| String as handler | Function reference |
| Lowercase (onclick) | CamelCase (onClick) |
| Uses native event | Uses SyntheticEvent |
Common React Events
| Event Type | React Event | Example |
|---|---|---|
| Mouse | onClick | <button onClick={fn}> |
| Form | onChange | <input onChange={fn}> |
| Form | onSubmit | <form onSubmit={fn}> |
| Keyboard | onKeyDown | <input onKeyDown={fn}> |
| Keyboard | onKeyUp | <input onKeyUp={fn}> |
| Focus/Blur | onFocus, onBlur | <input onFocus={fn}> |
| Clipboard | onCopy, onPaste | <input onPaste={fn}> |
| Touch | onTouchStart | <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
| Concept | Description |
|---|---|
| onClick, onChange | Most common React events |
| SyntheticEvent | React’s cross-browser event system |
| e.preventDefault() | Prevent default browser behavior (like form submit) |
| Event handler | Should be a function (not a string) |



Comments
Add new comment