8 min read

React Suspense: Lazy Loading for a Smoother UX

React Suspense: No More Loading Spinners of Doom!

Let’s be honest, nobody likes staring at a loading spinner while waiting for a website to load. It’s like watching paint dry – except the paint is your web page, and the dryness is your user’s patience. React Suspense is here to save the day (and your users’ sanity) by enabling lazy loading, which means loading components and data on demand, not all at once. This post will dive into the practical uses of Suspense, showing you how to improve initial load times and create a smoother, more responsive user experience.

Lazy Loading: The Key to Performance Nirvana

Lazy loading is like ordering food at a restaurant. You don’t get everything on the menu at once; you order dishes as you need them. Similarly, with lazy loading, components and data are loaded only when they are needed, not when the page initially loads. This can drastically improve initial load times, especially for large and complex applications.

Suspense in Action: Code Examples (TypeScript Style)

Suspense is incredibly easy to use. Wrap the component you want to lazy-load in a React.lazy() call and then wrap that in a <Suspense> component:

import React, { Suspense } from "react";

// Lazy-load the OtherComponent
const OtherComponent = React.lazy(() => import("./OtherComponent"));

function MyComponent() {
  return (
    <div>
      <Suspense fallback={<div>Loading...</div>}>
        <OtherComponent />
      </Suspense>
    </div>
  );
}

The fallback prop of the Suspense component specifies what to display while the OtherComponent is loading. This could be a loading spinner, a placeholder, or anything you want.

Real-World Example: Lazy Loading a Heavy Component

Imagine you have a component that renders complex charts and visualizations. This component is heavy and takes a while to load. You can use Suspense to load it only when it’s needed:

import React, { Suspense, useState, useEffect } from "react";

// Lazy-load the ChartComponent
const ChartComponent = React.lazy(() => import("./ChartComponent"));

function Dashboard() {
  const [data, setData] = useState(null);

  useEffect(() => {
    async function fetchData() {
      const result = await fetch("https://example.com/api/data");
      const json = await result.json();
      setData(json);
    }
    fetchData();
  }, []);

  if (!data) {
    return <div>Loading Dashboard...</div>;
  }

  return (
    <div>
      <h1>Dashboard</h1>
      <Suspense fallback={<div>Loading Charts...</div>}>
        <ChartComponent data={data} />
      </Suspense>
    </div>
  );
}

In this example, the ChartComponent is loaded only when the dashboard data is available, providing a smooth user experience.

Data Fetching with Suspense: Keeping It Asynchronous

Suspense also works with asynchronous data fetching. You can wrap your data fetching logic in a promise and Suspense will handle the loading state automatically.

import React, { Suspense, useState, useEffect } from "react";

interface Post {
  userId: number;
  id: number;
  title: string;
  body: string;
}

async function fetchPostData(): Promise<Post[]> {
  const res = await fetch("https://jsonplaceholder.typicode.com/posts");
  return res.json();
}

function PostList() {
  const [posts, setPosts] = useState<Post[] | null>(null);

  useEffect(() => {
    let ignore = false;
    async function startFetching() {
      const posts = await fetchPostData();
      if (!ignore) {
        setPosts(posts);
      }
    }
    startFetching();
    return () => {
      ignore = true;
    };
  }, []);

  if (!posts) {
    throw fetchPostData();
  }
  return (
    <ul>
      {posts.map((post) => (
        <li key={post.id}>
          <h3>{post.title}</h3>
        </li>
      ))}
    </ul>
  );
}

function MyComponent2() {
  return (
    <Suspense fallback={<div>Loading Posts...</div>}>
      <PostList />
    </Suspense>
  );
}

export default MyComponent2;

In this example, the PostList component throws a promise if the data hasn’t loaded yet. Suspense catches this promise and displays the fallback UI until the promise resolves.

Real-World Example: Lazy Loading Data with Suspense

Suppose you have a news feed that fetches articles asynchronously. You can use Suspense to handle the loading state smoothly:

import React, { Suspense, useState, useEffect } from "react";

interface Article {
  id: number;
  title: string;
  body: string;
}

async function fetchArticles(): Promise<Article[]> {
  const res = await fetch("https://example.com/api/articles");
  return res.json();
}

function ArticleList() {
  const [articles, setArticles] = useState<Article[] | null>(null);

  useEffect(() => {
    let ignore = false;
    async function startFetching() {
      const articles = await fetchArticles();
      if (!ignore) {
        setArticles(articles);
      }
    }
    startFetching();
    return () => {
      ignore = true;
    };
  }, []);

  if (!articles) {
    throw fetchArticles();
  }
  return (
    <ul>
      {articles.map((article) => (
        <li key={article.id}>
          <h3>{article.title}</h3>
          <p>{article.body}</p>
        </li>
      ))}
    </ul>
  );
}

function NewsFeed() {
  return (
    <div>
      <h1>News Feed</h1>
      <Suspense fallback={<div>Loading Articles...</div>}>
        <ArticleList />
      </Suspense>
    </div>
  );
}

export default NewsFeed;

In this example, the ArticleList component throws a promise if the articles haven’t loaded yet, and Suspense handles the loading state by displaying a fallback UI.

Route-Based Lazy Loading: Optimizing for Navigation

Suspense is especially useful for route-based lazy loading, where components for different routes are loaded only when the user navigates to those routes. This can greatly improve the performance of large applications with many routes.

Using React Router with Suspense

React Router integrates seamlessly with Suspense for lazy loading components:

import React, { lazy, Suspense } from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";

const Home = lazy(() => import("./Home"));
const About = lazy(() => import("./About"));

function App() {
  return (
    <Router>
      <Suspense fallback={<div>Loading...</div>}>
        <Switch>
          <Route exact path="/" component={Home} />
          <Route path="/about" component={About} />
        </Switch>
      </Suspense>
    </Router>
  );
}

export default App;

In this example, the Home and About components are loaded lazily when the respective routes are accessed.

Error Handling with Suspense: Catching Those Pesky Bugs

Suspense can also be used for error handling. If a lazy-loaded component throws an error during rendering, Suspense can catch the error and display a fallback UI. However, for more robust error handling, especially for errors that occur after the initial render, it’s best to use Error Boundaries (we’ll cover those in another post!).

Implementing Error Handling with Suspense

import React, { Suspense } from "react";
import { ErrorBoundary } from "react-error-boundary";

const OtherComponent = React.lazy(() => import("./OtherComponent"));

function ErrorFallback({
  error,
  resetErrorBoundary,
}: {
  error: Error;
  resetErrorBoundary: () => void;
}) {
  return (
    <div>
      <h1>Something went wrong!</h1>
      <p>{error.message}</p>
      <button onClick={resetErrorBoundary}>Try again</button>
    </div>
  );
}

function App() {
  return (
    <ErrorBoundary
      FallbackComponent={ErrorFallback}
      onReset={() => {
        // Reset the state of your app here
      }}
    >
      <Suspense fallback={<div>Loading...</div>}>
        <OtherComponent />
      </Suspense>
    </ErrorBoundary>
  );
}

export default App;

In this example, if OtherComponent throws an error, the ErrorBoundary will catch it and display a user-friendly error message.

Suspense and Concurrent Mode: A Powerful Combination

Suspense works particularly well with Concurrent Mode (now integrated into React 18 as the default mode), which allows React to interrupt rendering and prioritize more important updates, such as user interactions. This can lead to an even smoother and more responsive user experience.

How Concurrent Mode Enhances Suspense

Concurrent Mode enables React to perform more granular updates, allowing it to handle tasks like rendering, fetching data, and updating the UI more efficiently. This is particularly useful when combined with Suspense, as it allows React to prioritize rendering and loading tasks based on user interactions, improving the overall responsiveness of your application.

Best Practices for Using Suspense

  • Keep Fallbacks Small and Simple: Don’t put too much logic or complex UI in your fallbacks. The goal is to provide a quick and simple placeholder while the content is loading.
  • Use Suspense at Multiple Levels: Don’t just wrap your entire application in a single Suspense component. Use it strategically at different levels of your component tree to optimize loading for different sections of your UI.
  • Consider the User Experience: Think about what kind of fallback UI would be most appropriate for your application and your users. A loading spinner might be fine for some scenarios, but a skeleton loader or a placeholder might be better in others.

Example: Using Multiple Suspense Boundaries

import React, { Suspense, lazy } from "react";

const Header = lazy(() => import("./Header"));
const MainContent = lazy(() => import("./MainContent"));
const Footer = lazy(() => import("./Footer"));

function App() {
  return (
    <div>
      <Suspense fallback={<div>Loading Header...</div>}>
        <Header />
      </Suspense>
      <Suspense fallback={<div>Loading Main Content...</div>}>
        <MainContent />
      </Suspense>
      <Suspense fallback={<div>Loading Footer...</div>}>
        <Footer />
      </Suspense>
    </div>
  );
}

export default App;

In this example, each section of the app has its own Suspense boundary, allowing for more granular control over the loading state and providing a better user experience.

Conclusion: Lazy Loading for the Win!

React Suspense is a powerful tool for lazy loading components and data, which can significantly improve the performance and user experience of your applications. By using Suspense strategically and following best practices, you can create web apps that feel fast, responsive, and delightful to use. No more loading spinners of doom!