How to use events in ReactJS ? Last Updated : 25 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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:NodeJS or NPMReact JSApproach 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 handleOnSubmitIn 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-nameStep 2: Once it is done change your directory to the newly created application using the following command cd foldernameProject 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 startOutput: Comment More infoAdvertise with us Next Article How to use events in ReactJS ? krcpr007 Follow Improve Article Tags : Web Technologies ReactJS Geeks Premier League Geeks-Premier-League-2022 React-Questions 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 How to use ClickAwayListener Component in ReactJS ? Detect if a click event happened outside an element. Whenever the user clicks somewhere in the document, this listener works. Material UI for React has this component available for us, and it is very easy to integrate.Prerequisites:React JSNPM & Node.jsMaterial UIApproach:To use ClickAwayListene 2 min read How to get the enter key in ReactJS ? Let us create a React project and then we will create a UI that takes input from users. Users can interact with the UI and press Enter Key to trigger an event through this. Approach:Enter Key in React JS comes in the KeyDown Event. To get the enter key we check the event to verify which key is press 2 min read How to get an Element by ID in ReactJS ? ReactJS, a popular JavaScript library for user interfaces, empowers developers with tools, such as the ref system, to manipulate the DOM by accessing elements using their unique IDs. This article explores the process of obtaining elements by ID in ReactJS for effective component interaction and mani 4 min read How are events handled in React? 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, m 2 min read React onInput Event React onInput is an event handler that triggers then there is any change in the input field. It is fired immediately when the user is changing input. It is one of the form events that updates when the input field is modified. It is similar to the HTML DOM oninput event but uses the camelCase convent 2 min read Like