Pārlūkot izejas kodu

Run React app only when DOM content is loaded

Konstantin Tarkus 9 gadi atpakaļ
vecāks
revīzija
b526067910
1 mainītis faili ar 11 papildinājumiem un 1 dzēšanām
  1. 11 1
      app.js

+ 11 - 1
app.js

@@ -19,7 +19,7 @@ const route = async (path, callback) => {
   await callback(<Layout>{React.createElement(component)}</Layout>);
 };
 
-if (canUseDOM) {
+function run() {
   const container = document.getElementById('app');
   Location.listen(location => {
     route(location.pathname, async (component) => ReactDOM.render(component, container, () => {
@@ -29,4 +29,14 @@ if (canUseDOM) {
   });
 }
 
+if (canUseDOM) {
+// Run the application when both DOM is ready
+// and page content is loaded
+  if (window.addEventListener) {
+    window.addEventListener('DOMContentLoaded', run);
+  } else {
+    window.attachEvent('onload', run);
+  }
+}
+
 export default { route, routes };