onbeforeunload not working inside react component

后端 未结 1 1635
天涯浪人
天涯浪人 2021-01-14 00:52

I have a simple react-component where a user can edit data. As the values that may be changed could take some time I want to ask the user to confirm when leaving the page in

相关标签:
1条回答
  • 2021-01-14 01:45

    You'll need to call the method inside the lifecycle methods:

    componentDidMount() {
      window.addEventListener("beforeunload", this.handleWindowBeforeUnload);
    }
    
    componentWillUnmount() {
      window.removeEventListener("beforeunload", this.handleWindowBeforeUnload);
    }
    
    0 讨论(0)
提交回复
热议问题