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)