In this tutorial, we will learn how to handle events in React App.
Event Handling Overview
- React events are written in camelCase syntax: onClick instead of onclick.
- React event handlers are written inside curly braces: onClick={shoot} instead of onClick="shoot()"
React Event Handling Example
1. Create a new React app
npx create-react-app my-app2. Navigate into the app directory
cd my-app3. Start the development server
npm start4. Example 1: Create EventHandlingComponent Functional Component
import React, { useState } from 'react';
const EventHandlingComponent = () => {
const [count, setCount] = useState(0);
const handleClick = () => {
setCount(count + 1);
};
const handleReset = () => {
setCount(0);
};
return (
<div>
<h2>Event Handling Example</h2>
<p>Count: {count}</p>
<button onClick={handleClick}>Increment</button>
<button onClick={handleReset}>Reset</button>
</div>
);
};
export default EventHandlingComponent;The handleClick function is called when the "Increment" button is clicked. It updates the count state by incrementing it by 1 using the setCount function provided by the useState hook.
The handleReset function is called when the "Reset" button is clicked. It sets the count state back to 0.
Both the handleClick and handleReset functions are attached to the buttons using the onClick event handler attribute. When the buttons are clicked, the corresponding functions are executed, resulting in state updates and re-rendering of the component.
5. Example 2: Event handling in React for a Form
import React, { useState } from 'react';
const FormExample = () => {
const [formData, setFormData] = useState({
name: '',
email: '',
message: ''
});
const handleChange = (event) => {
const { name, value } = event.target;
setFormData((prevData) => ({
...prevData,
[name]: value
}));
};
const handleSubmit = (event) => {
event.preventDefault();
console.log(formData);
// You can perform further actions like submitting the data to a server
// or resetting the form fields.
};
return (
<div>
<h1>Form Example</h1>
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="name">Name:</label>
<input
type="text"
id="name"
name="name"
value={formData.name}
onChange={handleChange}
/>
</div>
<div>
<label htmlFor="email">Email:</label>
<input
type="email"
id="email"
name="email"
value={formData.email}
onChange={handleChange}
/>
</div>
<div>
<label htmlFor="message">Message:</label>
<textarea
id="message"
name="message"
value={formData.message}
onChange={handleChange}
></textarea>
</div>
<button type="submit">Submit</button>
</form>
</div>
);
};
export default FormExample;Related Tutorials
- React Hooks - useState and useEffect
- React JS ( React Hooks) + Spring Boot Tutorial
- Create a New React App Step by Step
- Understanding React App Folder Structure
- How to Add Bootstrap to React App
- ReactJS Axios GET, POST, PUT, and DELETE Example Tutorial
- ReactJS Axios Example
- ReactJS Fetch API Example
- React JS CRUD Example Tutorial
- React Router Step-By-Step Tutorial
- React Class Components
- React Functional Components
- React Props
- React State and setState in Class Components
- React useState Hook
- React Conditional Rendering
- How to Add Bootstrap in React JS Using NPM
- How to Create React App using Vite
- Handling Events in React
- How to Consume REST API in React
- React JS Form Validation Example
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment