next-dynamic.native.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import { createElement, lazy, Suspense } from 'react';
  2. /**
  3. * Native stand-in for `next/dynamic` (aliased in apps/expo/metro.config.js), so
  4. * registry `_map.js` files can use `dynamic(() => import('./x'))` on both platforms.
  5. *
  6. * Metro inlines `import()`, so there is no code splitting to gain on native. The
  7. * load starts when the map is evaluated and the component then renders
  8. * synchronously, like a static import; `React.lazy` + Suspense only covers a
  9. * render that comes before the import has settled.
  10. */
  11. export default function dynamic(loader, options = {}) {
  12. let Loaded = null;
  13. const promise = loader().then((mod) => {
  14. Loaded = mod && typeof mod === 'object' && 'default' in mod ? mod.default : mod;
  15. return { default: Loaded };
  16. });
  17. const Lazy = lazy(() => promise);
  18. const Loading = options.loading;
  19. function DynamicComponent(props) {
  20. if (Loaded) {
  21. return createElement(Loaded, props);
  22. }
  23. return createElement(
  24. Suspense,
  25. { fallback: Loading ? createElement(Loading) : null },
  26. createElement(Lazy, props)
  27. );
  28. }
  29. return DynamicComponent;
  30. }