Giter Site home page Giter Site logo

Comments (6)

LarsVoith avatar LarsVoith commented on May 17, 2024 1

I'm supposed to make a presentation to our dev team in October on F7 and React, I got a few hours to burn through yet so give it a try and let you know how it turns out.

from framework7-template-react.

bencompton avatar bencompton commented on May 17, 2024

I have never tried code splitting / dynamic imports in Create React App, but it supposedly works. It would be nice if just importing something with a dynamic imports like import('dynamically-loaded-page').then(...) automatically caused the build process to generate more than one JavaScript file and then dynamically load portions of your app on demand. In my experience with other build tools, it isn't always easy to get it to work. If you end up trying it, I'd be curious to hear how it worked out for you.

from framework7-template-react.

LarsVoith avatar LarsVoith commented on May 17, 2024

So far I have been able to import a component when the page loads after adding
an AsyncComponent - see 3.chunk.js. However you can't navigate back because the class for previous and the new current page is missing. Maybe
a <Switch in routes.js would work better? (My Code is at the bottom)

snip20180916_4

Normally we would expect to see classes previous and current.

snip20180916_5

class for previous page is missing and the class for the new current page is missing.

snip20180916_3

routes.js

 import asyncComponent from "./components/AsyncComponent";

 import NotFoundPage from './components/pages/NotFoundPage';

 import AboutPage from "./components/pages/AboutPage";

 const AsyncHome = asyncComponent(() =>import("./components/pages/HomePage"));

 const DynamicRoutePage = asyncComponent(() =>import("./components/pages/DynamicRoutePage"));

export default [{
   path: '/',
   component: AsyncHome,
 },
 {
    path: '/dynamic-route/blog/:blogId/post/:postId/',
    component: DynamicRoutePage,
  },
 {
    path: '/about/',
    component: AboutPage,
  },
  {
    path: '(.*)',
    component: NotFoundPage,
  }
];

AsyncComponent

 import React, { Component } from "react";

 export default function asyncComponent(importComponent) {

  class AsyncComponent extends Component {

    constructor(props) {
      super(props);

      this.state = {
        component: null
      };
    }

    async componentDidMount() {
      const { default: component } = await importComponent();

      this.setState({
        component: component
      });
    }

    render() {
      const C = this.state.component;

      return C ? <C {...this.props} /> : null;
    }
  }

  return AsyncComponent;
}

from framework7-template-react.

bencompton avatar bencompton commented on May 17, 2024

It appears to at least be splitting into separate JavaScript files correctly, which is pretty awesome. I would recommend handling the async loading in your routes or navigation logic instead of in your React components. For example, you could try using async routes. I have never tried them myself, but it's worth a shot. This is the basic idea anyhow:

routes = [
  {
    path: '/foo/',
    async(routeTo, routeFrom, resolve, reject) {
      const DynamicRoutePage = await import("./components/pages/DynamicRoutePage");
      resolve({ component: DynamicRoutePage });
    }
  }
]

from framework7-template-react.

LarsVoith avatar LarsVoith commented on May 17, 2024

Ok sometimes it helps to read the docs .

async-lazy-components
http://framework7.io/react/navigation-router.html#async-lazy-components

I can validate at this point that it is working in the template.

from framework7-template-react.

bencompton avatar bencompton commented on May 17, 2024

Ah, I didn't see that either. Nice job getting it working, and thanks for sharing your experiences!

from framework7-template-react.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.