React Core & Hooks (407 Qs)•Part 8: Advanced React Patterns, Portals & Performance (152 - 261)
What is React lazy function?
junior LevelCode ExampleFormsPerformance
The React.lazy() function lets you render a dynamic import as a regular component. This enables code-splitting by loading the bundle containing the component only when it is actually rendered on screen. It must be wrapped inside a React.Suspense boundary:
jsx
import React, { Suspense, lazy } from 'react';
const HeavyComponent = lazy(() => import('./HeavyComponent'));
function MyPage() {
return (
<Suspense fallback={<div>Loading component...</div>}>
<HeavyComponent />
</Suspense>
);
}