react js : detect page refresh
16,205
The event beforeunload is executed just before window browser is being refreshed. Event is cancellable.
window.beforeunload = (e) => {
console.log('Stop this');
e.preventDefault()
e.returnValue = '';
};
When using React an option is to take control in your Application component, or your most higher order component, in the componentDidMount
lifecycle method as @georgim suggested instead componentWillUnmount
as I first suggested, and manage there what you want to achieve.
Author by
learning_vba
Updated on August 02, 2022Comments
-
learning_vba almost 2 years
I am new to react.js. I have a simple page with table. When I reload the page, state is getting lost. Is there a way to detect the browser refresh ?
-
Kokodoko over 6 yearsSingle page apps shouldn’t reload the whole page, just if you don’t want to hard-resets your state.
-
learning_vba over 6 yearsits not a single page app.
-
Hamza Baig over 6 years@learning_vba You don't need
renderTable();
in yourMainHandler.js
Because that'll be handled byDashboard.js
independently. -
learning_vba over 6 yearsthing is I dont want to change the existing logic. Is there anyway to detect browser refresh?
-
-
Giorgi Moniava over 5 yearswhat do you think about componentDidMount in the top level component? (to detect reload)
-
Dez over 5 years@giorgim I have been trying it and besides it is better to use the
beforeunload
event listener, indeed it is more reliable to apply the logic incomponentDidMount
. -
Anxifer over 2 yearsthis API is deprecated