What is React lazy function?
The React.lazy
function lets you render a dynamic import as a regular component. It will automatically load the bundle containing the OtherComponent
when the component gets rendered. This must return a Promise which resolves to a module with a default export containing a React component.
const OtherComponent = React.lazy(() => import("./OtherComponent"));
function MyComponent() {
return (
<div>
<OtherComponent />
</div>
);
}
caution
React.lazy
and Suspense
is not yet available for server-side rendering. If you want to do code-splitting in a server rendered app, we still recommend React Loadable.