React lazy components

From wikinotes
Revision as of 01:34, 11 December 2022 by Will (talk | contribs) (Created page with " = Lazy Component Imports = <blockquote> A component import can be wrapped in a <code>React.lazy()</code>, which delays the component from loading until it is called.<br> While it is loading, the nearest parent <code>React.Suspense</code> component will be rendered in it's place (ex. loading-bar, spinner). <syntaxhighlight lang="javascript"> const MyComponent = React.lazy(() => import('./MyComponent')); function RootComponent() { return ( <React.Suspense fallbac...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Lazy Component Imports

A component import can be wrapped in a React.lazy(), which delays the component from loading until it is called.
While it is loading, the nearest parent React.Suspense component will be rendered in it's place (ex. loading-bar, spinner).

const MyComponent = React.lazy(() => import('./MyComponent'));

function RootComponent() {
  return (
    <React.Suspense fallback={<Spinner />}>
      <div>
        <MyComponent />
      </div>
    </React.Suspense>
  )
}

Transitions