lazy
JSAny
lazy(
Creates a lazy-loaded component using dynamic imports.
The load function should return a Future that resolves to a component.
The lazy component should be rendered inside a Suspense component.
Example:
// Define lazy component
final LazyComponent = lazy(() async {
// Simulate loading delay
await Future.delayed(Duration(seconds: 1));
return MyHeavyComponent;
});
// Use with Suspense
suspense(
fallback: pEl('Loading...'),
child: createElement(LazyComponent, props),
);
Implementation
JSAny lazy(Future<JSAny> Function() load) {
Future<JSObject> jsLoad() async {
final component = await load();
// React.lazy expects a module with a 'default' export
final module = JSObject();
module['default'] = component;
return module;
}
return _reactLazy((() => jsLoad().toJS).toJS);
}