bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/React/React Core
React•React Core

React Suspense

React Suspense lets you display an alternative HTML while waiting for code or data to load.

The alternative HTML can be a component, text, or any valid content.

What is Suspense?

Suspense is a React feature that lets your components display an alternative HTML while waiting for code or data to load.

The most common use cases are

  • Data fetching with suspense-enabled frameworks
  • Loading components dynamically with React.lazy()

Using Suspense

If a component takes time to load, you can use a Suspense component, and it will display the fallback content while the component is loading.

Example

The Fruits component takes two seconds to load, so we wrap it in a Suspense component to display a loading message while it is loading.

import { createRoot } from 'react-dom/client';
import { Suspense } from 'react';
import Fruits from './Fruits';
function App() {
 return ( <div> <Suspense fallback={<div>Loading...</div>}> <Fruits /> </Suspense> </div> );
}
createRoot(document.getElementById('root')).render( <App /> );

Using Suspense with lazy Loading

Another common use of the Suspense component is when importing components with lazy loading:

In the example above we had to fake a delay of two seconds to see the loading message. A task like displaying three fruits from an array would be too fast to see the loading message at all.

But with lazy loading, we can import a component dynamically, and it will display a loading message while it is loading, even if the task is very fast.

Lets first create an example WITHOUT using lazy loading, where we do not fake a two seconds delay:

Example

This example is too fast to see the loading message:

import { createRoot } from 'react-dom/client';
import { Suspense } from 'react';
import Cars from './Cars';
function App() {
 return ( <div> <Suspense fallback={<div>Loading...</div>}> <Cars /> </Suspense> </div> );
}
createRoot(document.getElementById('root')).render( <App /> );

Now let us create an example WITH using lazy loading:

Example

Same example as above, but using lazy loading:

import { createRoot } from 'react-dom/client';
import { Suspense, lazy } from 'react';
const Cars = lazy(() => import('./Cars'));
function App() {
 return ( <div> <Suspense fallback={<div>Loading...</div>}> <Cars /> </Suspense> </div> );
}
createRoot(document.getElementById('root')).render( <App /> );

Example Explained

  • lazy() lets you load a component dynamically
  • Suspense shows a fallback while the component loads

Multiple Components

One Suspense component can wrap multiple lazy components:

Example

import { createRoot } from 'react-dom/client';
import { Suspense, lazy } from 'react';
const Header = lazy(() => import('./Header'));
const Content = lazy(() => import('./Content'));
const Sidebar = lazy(() => import('./Sidebar'));
function App() {
 return ( <div> <Suspense fallback={<div>Loading...</div>}> <Header /> <div style={{ display: 'flex' }}> <Sidebar /> <Content /> </div> </Suspense> </div> );
}
createRoot(document.getElementById('root')).render( <App /> );

Previous

React Portals

Next

Styling React Using CSS

This chapter

Overview
45

Lessons

142m

Read time

1. React Tutorial2. React Introduction3. React Getting Started4. Your First React App5. React Render HTML6. Upgrade React7. React ES68. React ES6 Classes9. React ES6 Arrow Functions10. React ES6 Variables11. JavaScript Array map()12. React ES6 Destructuring13. React ES6 Spread Operator14. React ES6 Modules15. React ES6 Ternary Operator16. React ES6 Template Strings17. React JSX18. React JSX Expressions19. React JSX Attributes20. React JSX If Statements21. React Components22. React Class Components23. React Props24. React Destructuring Props25. React Props Children26. React Events27. React Conditional Rendering28. React Lists29. React Forms30. React Submit Forms31. React Forms - Textarea32. React Forms - Select33. React Forms - Multiple Input Fields34. React Forms - Checkbox35. React Forms - Radio36. React Portals37. React Suspense38. Styling React Using CSS39. React CSS Modules40. React CSS-in-JS41. React Router42. React Transitions43. React forwardRef44. React Higher Order Components45. React Sass Styling

On this page

What is Suspense?Using SuspenseUsing Suspense with lazy LoadingExample ExplainedMultiple Components