0

So, I need to fire a rest api call whenever a reload of the page, change the page, closing the window or tab event happens

For this, I have followed this post

reactjs event listener beforeunload added but not removed

It works with page reload and changing the page, but not on tab or window close.

I don't want to show a confirm message, just make the api call

class comp extends Component {
  constructor(props) {
    super(props);
    this.handleWindowClose = this.handleWindowClose.bind(this);
  }

  componentDidMount() {
    window.addEventListener('beforeunload', this.handleWindowClose);
  }

  componentWillUnmount() {
    window.removeEventListener('beforeunload', this.handleWindowClose);
  }

  handleWindowClose(event) {
    event.preventDefault();
    if (!this.props.locked)
      unlockItem(this.props.id, this.props.version);
  }

  render() {
  }
}

Any suggestion?

CJLopez
  • 5,495
  • 2
  • 18
  • 30

0 Answers0