Home > Back-end >  Unable to page refresh when I add window.addEventListener inside the component class
Unable to page refresh when I add window.addEventListener inside the component class

Time:04-17

I have issues refreshing the page when I have window.addEventListener snippet in the component class.

componentDidMount() {
    window.addEventListener("keydown" , function(event) {
        event.preventDefault();
        if (event.keyCode === 13) {
            document.getElementById("searchLink").click();
        }
    });
}

Note: ctrl f5 doesn't work, all keyboard actions fail to work when I have this snippet enabled. even the chrome console is not opening with the keyboard shortcut. Refreshing the page with mouse pointer also doesn't work in the browser.

CodePudding user response:

componentDidMount() {
    window.addEventListener("keydown" , function(event) {
        if (event.key === "Enter") {
            event.preventDefault();
            document.getElementById("searchLink").click();
        }
    });
}

preventDefault is canceling all keyboard key press

  • Related