Explorar o código

Run React app only when DOM content is loaded

Konstantin Tarkus %!s(int64=9) %!d(string=hai) anos
pai
achega
b526067910
Modificáronse 1 ficheiros con 11 adicións e 1 borrados
  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 };