D-Money
D-Money

Reputation: 2465

Is it necessary to wait for window load event before initialising a ReactJS app?

Is there any reason to wait for a window load event before initialising a react app?

window.addEventListener('load', () => {
   ReactDOM.render(
     <h1>Reactastic!</h1>,
     document.getElementById('rootElement')
   );
}, false);

As long as rootElement is defined in html, not hidden or created dynamically by javascript, is there any possibility that it won't be available in time for React to init?

Edit: This is also assuming that the javascript is loaded in the footer.

Upvotes: 2

Views: 2697

Answers (2)

Massimiliano Janes
Massimiliano Janes

Reputation: 5624

the ReactDom renderer will mount everything in the container element, so, the only thing react needs(*) is an existing 'root' element at render call. Given your assumptions, this holds true hence you don't need to wait onload() in this case.

(*)of course, any script evaluated after rendering() invasively manipulating the dom inside your 'root' element can ( and probably will ) break the react logic ...

Upvotes: 1

Assuming the javascript is loaded at the bottom of the page, there is no reason to wait for the document to be loaded since the rootElement will already be available in the DOM.

Upvotes: 1

Related Questions