M
M
Marat Ivanov2019-05-27 15:48:56
JavaScript
Marat Ivanov, 2019-05-27 15:48:56

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`);
    }

Can't test. Tell

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-05-27
@mrair

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

handleWindowBeforeUnload = e => {
  // e - целевое событие
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question