DEV Community

vedant-jain03
vedant-jain03

Posted on

 

Add key event listener in React.

It is very simple in react, you can add any key event listener. I have used example of "Enter".

I have defined a sample function for the explaination.

    const handleInputEnter = (e) => {
        // You can 
        if(e.code === 'Enter') {
            console.log("Hello");
        }
    }
Enter fullscreen mode Exit fullscreen mode

Add onKeyUp attribute in the element.
Example:

<input type="text" onKeyUp={handleInputEnter}  />
Enter fullscreen mode Exit fullscreen mode

This will work completely fine.
Follow me on twitter: https://twitter.com/vedantj_03

Top comments (0)

16 Libraries You Should Know as a React Developer

Being a modern React developer is not about knowing just React itself. To stay competitive, it is highly recommended to explore the whole ecosystem. This article contains some of the most useful React component libraries to speed up your developer workflow.