You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BREAKING CHANGE:
- `ErrorComponent` is ignored, please use Error Boundaries to handle errors.
- `lazy` is no longer exported
- `LoadingComponent` is replaced by `fallback` option
- `ref` are now forwarded
@@ -23,6 +23,18 @@ Loadable leverage the limit of Code Splitting and give you access to all feature
23
23
24
24
Code Splitting + Server Side Rendering is something very complex. Several libraries tried to solve this problem successfully or not. The goal of this library is to follow as much as possible the philosophy of React and give a developer-experience first solution to solve this complex problem. It takes the best from all libraries and provide an elegant solution to this problem.
25
25
26
+
## Differences with React.lazy & react-loadable
27
+
28
+
[`React.lazy`](https://reactjs.org/docs/code-splitting.html#reactlazy) doesn't support full dynamic import and SSR. Loadable uses the same API with more features (SSR, full dynamic import, library import). If you don't need them, you don't need `loadable`.
Even if [`react-loadable` is recommended by React team](https://reactjs.org/docs/code-splitting.html#reactlazy), the project does not accept any GitHub issues and is no longer maintained.
37
+
26
38
## Getting started
27
39
28
40
`loadable` lets you render a dynamic import as a regular component.
@@ -41,6 +53,53 @@ function MyComponent() {
41
53
}
42
54
```
43
55
56
+
### Loading library
57
+
58
+
`loadable.lib` lets you defer the loading of a library. It takes a render props called when the library is loaded.
59
+
60
+
```js
61
+
importloadablefrom'@loadable/component'
62
+
63
+
constMoment=loadable.lib(() =>import('moment'))
64
+
65
+
functionMyComponent() {
66
+
return (
67
+
<div>
68
+
<Moment fallback="xx:xx">
69
+
{({ default: moment }) =>moment().format('HH:mm')}
70
+
</Moment>
71
+
</div>
72
+
)
73
+
}
74
+
```
75
+
76
+
You can also use a `ref` that will be populated when the library will be loaded.
<button onClick={this.handleClick}>What time is it?</button>
96
+
<Moment ref={this.moment} />
97
+
</div>
98
+
)
99
+
}
100
+
}
101
+
```
102
+
44
103
### Full dynamic import
45
104
46
105
Webpack accepts [full dynamic imports](https://webpack.js.org/api/module-methods/#import-) and you can also use them with `@loadable/component` to create dynamic components.
@@ -62,12 +121,12 @@ function MyComponent() {
62
121
63
122
### Suspense
64
123
65
-
`@loadable/component` exposes a `lazy` method that acts similarly as `React.lazy` one.
124
+
`@loadable/component` exposes a `loadable.lazy` method that acts similarly as `React.lazy` one.
If the other module fails to load (for example, due to network failure), it will trigger an error. You can handle these errors to show a nice user experience and manage recovery with [Error Boundaries](https://reactjs.org/docs/error-boundaries.html). Once you’ve created your Error Boundary, you can use it anywhere above your lazy components to display an error state when there’s a network error.
0 commit comments