Преглед изворни кода

Run React app only when DOM content is loaded

Konstantin Tarkus пре 9 година
родитељ
комит
b526067910
1 измењених фајлова са 11 додато и 1 уклоњено
  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 };