📚Cheatsheets

Cheatsheet collection for go, rust, python, shell and javascript.

Listen Event on key Enter

If you want to listen for the key Enter event in React, you can use the keydown event handler. This will fire whenever the user presses the Enter key, and you can then do whatever you need to do in your event handler. Here's how to register on key down listener for Enter key.

useEffect(() => {
    const keyDownHandler = (e) => {
        if (e.code == "Enter") {
            // do something
        }
    };

    document.addEventListener("keydown", keyDownHandler);
    return () => {
        document.removeEventListener("keydown", keyDownHandler);
    };
}, []);