Giter Site home page Giter Site logo

Code Splitting about react HOT 4 CLOSED

ui-router avatar ui-router commented on April 19, 2024
Code Splitting

from react.

Comments (4)

elboman avatar elboman commented on April 19, 2024

Sure! UI-Router Core 1.0 introduced future states and lets you lazy load them when needed.
All you need to do is declare the future state:

{
  name: 'lazy',
  url: '/this-is-lazy',
  lazyLoad: () => System.import('./lazyLoadState')
}

The lazyLoadState file must declare the state that will replace with the future-state (that acts as a placeholder) and export a default object with a states attribute.

import MyComponent from './MyComponent';
const state = {
  name: 'lazy',
  url: '/this-is-lazy',
  component: MyComponent
};
export default { states: [state] }

Note that the syntax for lazy loading a JS module depends on the bundler you use. Since the lazyLoad function in your state declaration requires you to return a promise, verbosity may change drastically based on your module bundler: Webpack 2 supports System.import() as well as import(), while webpack 1.x supports the verbose require.ensure() syntax which isn't promise-based.

This doesn't prevent you from writing a small helper function to reduce this verbosity:

const lazyLoadModule = (moduleName) => new Promise((resolve, reject) => {
  require.ensure([], () => { resolve(require(moduleName).default) });
});

const state = {
  name: 'lazy',
  url: '/this-is-lazy',
  lazyLoad: () => lazyLoadModule('./lazyLoadState')
}

In any case I wrote a small working example, hope it helps: http://www.webpackbin.com/Ekiu1rHNz

Also, you can take a look at the core documentation regarding future states here: https://ui-router.github.io/docs/latest/interfaces/state.statedeclaration.html#lazyload

Let me know if you have any doubt

from react.

MichaelBuen avatar MichaelBuen commented on April 19, 2024

http://www.webpackbin.com/Ekiu1rHNz link is no longer working

image

I tried the code-splitting with @uirouter/react. The chunk is loading in network tab, however for some reason the component is not rendering in the page. @uirouter/react works if there's no code-splitting. if I put the component directly on state's component property. Is there any plugins that I missed or something?

from react.

MichaelBuen avatar MichaelBuen commented on April 19, 2024

I'm thinking the code-splitting's problem is caused by using module: commonjs in tsconfig.json. However if I set it to umd, TypeScript complains:

image

Here's my router states, app component is loaded via _import:

const states = [{
    name    : 'home.**',
    url     : '/home',
     // component: App
    lazyLoad: () => _import('../components/App')
}];

Here's the components/App:

window.alert('yay');

const state = {
    name: 'home',
    url: '/home',
    component: App
};

export default { states: [state] };

The chunk loads, window.alert('yay') run. The window.alert in App's constructor didn't though. It seems that the component property of future state is not being swapped to original state.

Is there a plunker or webpackbin example with code-splitting usage, and also with otherwise?

I have good experience with ui-router in Angular 1.x and lazy-loading with it using ocLazyLoad. I'm thinking if I can use @uirouter/react with lazy-loading despite being 0.5.0 version.

from react.

brendanmckeown avatar brendanmckeown commented on April 19, 2024

@MichaelBuen and for anyone else who stumbles upon this issue, I fixed this by exporting a states array from the lazy-loaded module, instead of exporting the default.

const state = {
    name: 'home',
    url: '/home',
    component: App
};

export const states = [state];

from react.

Related Issues (20)

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.