DEV Community

Lakhan Samani
Lakhan Samani

Posted on

7

How to use React Suspense and lazy for code splitting

React Suspense and lazy is a great way to split your code ๐Ÿš€. It does all the magic and splits your codes into smaller bundle files. This would help in reducing the load time on the frontend and will load chunk only when the component/page is rendered. I am writing this post to share my learning about the Suspense and lazy, which can add flickers to your UI if not used correctly.

I have been using it with various other projects where I had to split code into chunks based on the routes. So a common practice to do that would be

const Home = React.lazy(() => import('./Home'));
const Messages = React.lazy(() => import('./Messages'));

const App = () => (
 <Suspense fallback={<Loading />}>
    <Switch>
      <Route path="/">
        <Home />
      </Route>
      <Route path="/messages">
        <Messages />
      </Route>
    </Switch>
 </Suspense>
)
Enter fullscreen mode Exit fullscreen mode

and that works completely fine. It will create 2 .js chunks, one for Home component and another one for the Messages component.

But what happens when you start importing components lazily in the Home component or Messages component. It would still work but you might see UI keeps flickering with the Loading component. If the generated chunks are smaller in size, the UI flickers look bad, as before even rendering the Loader component, it would hide that and render the new component.

Example

Click on individual buttons in the above example and observe the flickers that occur to the previously loaded Messages.

Why it flickers?

With the help of my friend Divyanshu I was able to learn and debug the reason behind this flicker. It happens because we have wrapped the Messages component in single Suspense which is at the root level, i.e. App.js. So whenever a child component is lazily loaded, the entire Messages component is suspended.

Since the loading happens pretty fast the fallback is rendered very briefly which causes a flicker (entire Messages component is suspended and then resumed back).

How to stop UI flickers?

There are 2 approaches to this.

  1. Use Suspense with each of the lazily loaded components, this means it will only suspend the state for that particular component

Example

  1. While discussing this issue with another friend Yash Joshi one more solution came up, i.e. to preload a component in componentDidMount or useEffect.

Example

Great learning over the weekend ๐ŸŽ‰ #reactjs #optimizations #learninpublic

Image of Stellar post

๐Ÿš€ Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup ๐Ÿš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

๐Ÿ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere โ€œthank youโ€ often brightens someoneโ€™s dayโ€”share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay