重新加载/关闭窗口时发生火灾事件

时间:2018-03-21 21:46:15

标签: javascript reactjs

所以,每当重新加载页面,更改页面,关闭窗口或标签事件时,我都需要触发一个休息api调用

为此,我已经关注了这篇文章

reactjs event listener beforeunload added but not removed

它适用于页面重新加载和更改页面,但不适用于选项卡或窗口关闭。

我不想显示确认消息,只需拨打api电话

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() {
  }
}

有什么建议吗?

0 个答案:

没有答案