I am trying to send information to the backend regarding the state of the application at page reload/close. Is there a way to detect these two actions and call an API before proceeding?
You can use thew window.unload event (https://developer.mozilla.org/en-US/docs/Web/Events/unload).
Put the code below in some point of you code (I suggest in the index.html or some entry point of the app):
window.addEventListener('unload', function(event) {
//call function to save you state in API or save in localStore
localStore.setItem('state', JSON.stringify(store.getState())
});
I think use componentWillUnmount could not resolve properly your problem, because react could call that event though the browser is not refresh or close.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With