Answer the question
In order to leave comments, you need to log in
How to catch react.js page close event?
Hello everyone, how is it correct to catch the closing and updating of the page in react.js?
async componentDidMount() {
const { item } = this.props;
document.body.classList.add('no-scroll');
this.props.resetScenario();
await service.startScenario(item.id);
this.props.startScenario();
window.addEventListener("beforeunload", this.handleWindowBeforeUnload);
}
componentWillUnmount() {
document.body.classList.remove('no-scroll');
window.removeEventListener("beforeunload", this.handleWindowBeforeUnload);
}
handleWindowBeforeUnload() {
service.sendEndScenario();
};
//service:
sendEndScenario(){
return axios.get(`${REST_URL}force_scenario_stop`);
}
Answer the question
In order to leave comments, you need to log in
componentDidMount() {
window.addEventListener('beforeunload', this.handleWindowBeforeUnload);
}
handleWindowBeforeUnload = e => {
// e - целевое событие
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question