Browser back button using React

后端 未结 4 1338
醉话见心
醉话见心 2020-12-16 22:27

I am using React. I want to warn the user when the user clicks on the back button.

What I had done was

handleWindowClose = (ev) => {
    ev.preventD         


        
4条回答
  •  误落风尘
    2020-12-16 22:58

    Try this code:

    componentDidMount() {
        window.addEventListener("popstate", this.onBackButtonEvent)
    }
    
    componentWillUnmount() {
      window.removeEventListener("popstate", this.onBackButtonEvent)
    }
    
    onBackButtonEvent = () => {
        window.history.forward()
    }
    

提交回复
热议问题