1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Import Components Lazily with Suspense React.lazy

    reactReact
    ^0.0.0-experimental-b53ea6ca0

    The Suspense component isn't new. We've been able to use it since 2018.

    Learn the simplest way to start using Suspense in any React codebase (v16.6 or later), using dynamic imports.

    Code

    Code

    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: I want to easy you in the Suspense. To do that, we'll take something you've done 1,000 times, which is import a component, and make it a little bit more suspensy.

    To do that, we'll follow a blog post that was written all the way back in October 2018 for the version 16.6 of React. The feature is code splitting. Simply by changing from a static import to a dynamic import, we can get our hands wet with Suspense. Let's dive in.

    First things first, we need to change the import statement. We'll call it as a function using the module path as an argument, PokemonDetail. The result of that import will be assigned to a constant with our desired name for that component, PokemonDetail.

    There's just one last piece. This dynamic import creates a promise. That promise will have pending error and success statuses. We need to wrap this generic promise in a function that will communicate with Suspense boundaries. That function is React.lazy(). We can copy and paste that here.

    React.lazy expects a function, so we can use a handy dandy aerial function here and close our parens. Now that we have our dynamic import in place, we can comment this one out or delete it completely. Let's mozy on back to our app and see what happened.

    Kaboom, the app blew up, but React gives us a great error telling us exactly what we need to do when using lazily loaded components. Let's read it. Error. "A React component suspended while rendering, but no fallback UI was specified. Add a component higher in the tree to provide a loading indicator or placeholder to display."

    We just need to follow steps. Add a component higher in the tree. Top and bottom, check. Provide a fallback prop to display a placeholder. For now, we can just use the text "Loading Pokemon." Save and fixed.

    Let's check our work. In the browser, open up the dev tools. Go to the network pane and filter on JS. Now, hit refresh. There are number of files here, but there are two that I want to point out.

    First is the main chunk. It's all compiled and ugly, but you can see in here hints of this main application component. What you won't see is anything related to the PokemonDetail. That came in a separate chunk. This chunk is splitting on that component boundary for PokemonDetail and only has the code for this one component.

    But what about this fallback? Everything loaded so quickly that we never saw it. We can check for it in two ways. Staying in the network tab, change to a slow 3G connection. Hit refresh again. It will take much longer this time, but you'll see, for a split second, "Loading Pokemon" before the component comes in and saves the day.

    Of course, if you don't want to slow down all of your requests to a screeching halt, you can install the Chrome DevTools, choose the component that you'd like to see in a suspended state and click this little button. That stopwatch will suspend the component and show the Suspense boundary fallback.

    Congratulations are in order. By changing this from a static to a dynamic import, you've now used your first React Suspense component.