How to get the enter key in ReactJS ? Last Updated : 09 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 pressed from the e.key value. and define the event handler function for executing the corresponding task. Syntax:<input onKeyDown={(e) => { if (e.key === "Enter") handlerFuntion(); }}/>Steps to Create React Application:Step 1: Initialize the react project using this command in the terminal.npx create-react-app project_nameStep 2: After creating your react project move into the folder to perform different operations.cd project_nameProject Structure: Project_StructureExample: This example demonstrate the use of onKeyDown event to get the enter key and change the state using setState(). JavaScript // Filename - App.js import React, { Component } from "react"; class App extends Component { state = { message: "" }; render() { return ( <div> <p>Enter Your Message and Press Enter:</p> <input onKeyDown={(e) => { if (e.key === "Enter") { this.setState({ message: e.target.value }, () => { alert(this.state.message); }); } }} type="text" /> </div> ); } } export default App; Step to run the application: Open the terminal and type the following command.npm startOutput: Open your browser. It will by default open a tab with localhost running (http://localhost:3000/) and you can see the output shown in the image. Enter your message and press ENTER to see the welcome message.Detecting Enter Key Comment More infoAdvertise with us Next Article How to get the enter key in ReactJS ? N namankedia Follow Improve Article Tags : Web Technologies ReactJS 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 refs in React JS? React JS, a powerful and declarative JavaScript library for building user interfaces, provides a feature known as "refs" that allows developers to interact with the underlying DOM elements directly. Refs are generally used for the following purposes:Managing focus, text selection, or media playback. 4 min read How to use events in ReactJS ? 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 How to get text inside Text component onPress in ReactJS ? In this article, we will learn "How to get the text inside Text component onPress in ReactJS?". Problem Statement: Sometimes in an application, it is required to get the text inside certain components, in order to reuse the components again & again just by changing the text value. Approach: We w 4 min read How to get cell value on React-Table ? React-Table is a powerful library that allows you to create flexible and feature-rich tables in your React applications. It provides various functionalities to manipulate and interact with table data. One common requirement is to retrieve the value of a specific cell in React-Table. In this article, 3 min read Like