I have a script in my react app that I would like to run after the page is completely done loading.
I've tried listening for the window load like this in componentDidMount / componentDidUpdate:
document.onreadystatechange = function () {
if (document.readyState === 'complete') {
// do stuff
}
}
This works once when the app is first loaded, but not on subsequent loads.
I've also tried this:
window.onload = function () { alert("It's loaded!") }
But also the same result.
I've tried adding this alert in a before hook on the history:
browserHistory.listen((location) => {
window.onload = function () { alert("It's loaded!") }
}
This only works after I'm navigating though. Is there an after hook for browserHistory that will run after the page is loaded?
Bottomline, I'd like to know when the page is loaded in a react app on route change and on refresh so I can execute a script.
Thanks
ReactDOM.render(<MyElement />, document.getElementById('root'), () => { console.log('page is loaded'); })
The function runs once the page is loaded. – Deterrence