react location.replace error when triggered by keydown

36 Views Asked by At

React I'm developing a popup and I want to close it down with both a button and the esc keydown.

However, even if I use the same function for both, some reason the window.location.replace() part inside the keydown function just doesn't seem to work.

everything else(console log / removing cookies) work inside the function, just not the window.location.replace('url')

I really need to use only the replace function to turn the popup off(directing into a new url with no going back), why would this happen?

Is there some things to look out for keydown I don't know about?

useEffect(() => {
        window.addEventListener('keydown', handleKeyDown)

    

return () => {
        window.removeEventListener('keydown', handleKeyDown)
    }
}, [])

const temp = () => {
    Cookies.remove('rurl', { domain: '.domain.io' })
    Cookies.remove('rurl')
    console.log(window.location.pathname)//for testing
    window.location.replace(window.location.pathname)
    console.log(2)//this is for testing. this gets printed but not the replace
}

const handleKeyDown = async (e) => {
    if (e.keyCode !== 27) {
        return
    }
    temp()
}

Got no ideas.. I tried searching for things to look out while useing keydown, but none were found Even though I tried changing replace to href it wouln't work either.(I need to use replace though)

0

There are 0 best solutions below