Suspense is a React component that displays fallback content (such as a loading message or spinner) while a component is loading.
It is commonly used with React.lazy() for Lazy Loading.
Example:
import React, { lazy, Suspense } from 'react';
const About = lazy(() => import('./About'));
function App() {
return (
Loading...}>
);
}
export default App;
Advantages of Suspense:
- Displays a loading message while waiting.
- Improves user experience.
- Works with React Lazy Loading.
- Helps manage loading states.
- Makes code cleaner.
Disadvantages of Suspense:
- Requires proper fallback UI.
- Lazy-loaded components may take time to appear.
- Needs Error Boundaries for handling loading failures.
- Can make loading behavior more complex in large applications.