Lazy loading in React allows components to be loaded only when they are needed, speeding up the initial page load time. Here is how you can lazy load components in React:
import React, { Suspense } from 'react';
const LazyComponent = React.lazy(() => import('./LazyComponent'));
const App = () => {
return (
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
);
}
export default App;
const LazyComponent = lazy(() => import('./LazyComponent'));
import Loadable from 'react-loadable';
const LoadableComponent = Loadable({
loader: () => import('./LazyComponent'),
loading() {
return <div>Loading...</div>;
}
});
const App = () => {
return (
<LoadableComponent />
);
}
By lazy loading components in React, you can optimize the performance of your web application by only loading the necessary components when they are needed, rather than all at once.