How to use events in ReactJS ?
Last Updated :
25 Jul, 2024
Modern webpages rely on user interactions, triggering events like clicks or keypresses. React facilitates event handling with built-in methods, allowing developers to create listeners for dynamic interfaces and responses. JavaScript events, inherent to the DOM, use bubbling propagation by default, moving upwards from children to parent elements, and can be bound either inline or in external scripts.
Prerequisites:
Approach to use Events:
Handling events with React elements is very similar to handling events on DOM elements. They only have some syntax differences.
- React events are named using camelCase, rather than lowercase.
- With JSX you pass a function as the event handler, rather than a string.
In DOM:
<button onclick="printValues()"> Print</button>
In React:Â
<button onClick={printValues}> Print</button>
On submitting the form you can return false as in we do in HTML. In React JS You must call preventDefault() explicitly.
- In your, Default App.js Do the following code changes.
- Here we created an arrow function name as handleOnSubmit
- In function on we do console.warn("You clicked on submit function")
- Make a button and add event onclick={handleOnSubmit}
Steps to Create the React Application And Installing Module:
Step 1: Create a react application using the following command Â
npx create-react-app app-name
Step 2: Once it is done change your directory to the newly created application using the following command Â
cd foldername
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Example: Write done the following below code in the App.js
JavaScript
import "./App.css";
function App() {
const handleOnSubmit = (e) => {
e.preventDefault();
console.warn("You clicked on submit function")
};
return (
<>
<h1>This is React WebApp </h1>
<form action="">
<button type="submit" onClick={handleOnSubmit}>
submit
</button>
</form>
</>
);
}
export default App;
Step to run the application: Enter the following command to run the application.
npm start
Output:
Similar Reads
How to Use onKeyPress Event in ReactJS? The onKeyPress event in ReactJS occurs when the user presses a key on the keyboard but it is not fired for all keys e.g. ALT, CTRL, SHIFT, ESC in all browsers.ApproachTo use the onKeyPress event in React we will use the predefined onKeyPress event prop. We will be using a text input field, and pass
2 min read
How to create an event in React ? In the development of contemporary webpages, user interactions play a pivotal role. These interactions include mouse clicks, keypresses, or even uncommon events like connecting a battery to a charger. As developers, our task is to actively "listen" to these events and subsequently ensure that our ap
2 min read
How to bind event handlers in React? Binding event handlers in React is a fundamental concept that ensures interactive and responsive web applications. React, a popular JavaScript library for building user interfaces uses a declarative approach to handle events. This article explores various methods to bind event handlers in React, hig
3 min read
How to create components in ReactJS ? Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
How to create Calendar in ReactJS ? Creating a calendar in React JS can help in React projects like to-do lists, e-commerce sites, Ticket booking sites, and many more apps. It visualizes the day, month, and year data and makes an interacting User interface.PrerequisitesReact JSNode.js & NPMTo create a calendar in React JS we will
2 min read