Comments (4)
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.
http://www.webpackbin.com/Ekiu1rHNz link is no longer working
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.
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:
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.
@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)
- Can ui-router/react support React18 new features? HOT 6
- testing ui router with relative paths
- {location: 'replace'} doesn't work in stateService.go(stateName, params, {location: 'replace'}) HOT 1
- useSref: "target="_blank" ignored when link contains child elements HOT 1
- Is this project still under active development? HOT 3
- Use same UIRouter instance inside different React.render(...) HOT 1
- Typescript demo errors on `1.0.1` HOT 1
- animating-transitions demo errors on `1.0.1` HOT 1
- Router.start() called more than once HOT 1
- 404 on react-dom.js on the tutorial pages HOT 2
- Path duplication after page refresh when using UIRouter pushStateLocationPlugin HOT 4
- Params of type json are not deserialized properly HOT 5
- States not gets enabled if there are nested lazy loaded states HOT 2
- UIView Doesn't Render with Usage of `urlService.rules.when` HOT 4
- Scroll restoration HOT 3
- Cannot use useRouter HOT 4
- Jest/Enzyme Transition Rejection/TypeError HOT 1
- UISref Component ignores target="_blank" on child anchor tag HOT 3
- Mocking UiRouter Classes with Jest HOT 4
- useCurrentStateAndParams don't reacting on initial state HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.