I have a react component like :
import React, { PropTypes, Component } from 'react'
class MyComponent extends Component {
componentDidMount() {
window.addEventListener("beforeunload", function (event) {
console.log("hellooww")
event.returnValue = "Hellooww"
})
}
componentWillUnmount() {
window.removeEventListener("beforeunload", function (event) {
console.log("hellooww")
event.returnValue = "Hellooww"
})
}
render() {
return (
<div>
Some content
</div>
)
}
}
export default MyComponent
Here event lister is added to the component. When I refresh the page it gives me pop up asking to leave the page.
But when I go to another page and do refresh it again shows the same pop-up.
I am removing the eventListener
from the component on componentWillUnmount
. Then why it is not being removed?
How can I remove the beforeunload
event on other pages?