>Huzaima

Lazy Loading React Components using intersection observer

Huzaima KhanMay 23, 20234 min read

Leveraging the intersection observer for lazy loading react components can significantly enhance your application's performance. This method allows you to postpone the rendering of components until they become necessary. The react intersection observer is particularly useful for handling content that appears below the fold, which, if loaded initially, can hinder performance.

I highly recommend using this technique to lazy load hefty images and videos located below the fold. This approach not only improves your app's speed but also positively impacts your core web vitals. Thus, the combination of the react intersection observer and lazy loading react components provides an efficient solution for performance optimization.

What is intersection observer?

The Intersection Observer is an API in JavaScript that provides a powerful and efficient way to monitor the visibility and position of DOM elements relative to a viewport. In other words, it allows you to track when an element enters or exits the boundaries of the viewport, or a specified 'root' element.

One of the primary advantages of using the Intersection Observer API is that it provides a non-blocking, asynchronous way to monitor element visibility changes, making it a better choice for performance-sensitive tasks compared to traditional methods like scroll events or continually checking element positions with getBoundingClientRect.

Browser support for intersection observer

The intersection observer API is supported by most modern web browsers, including Chrome, Firefox, Safari, and Edge. However, it may not be supported by older browsers or some mobile browsers. You can check the specific browser support for the intersection observer API on caniuse.com, which tracks browser support for different web technologies. If your target browser don't support it, you can polyfill (add support for) the API in order to use it in your code.

Lazy Loading React Components using intersection observer (the hard way)

To use the intersection observer API in React, you can use the useRef hook to create a reference to the element you want to monitor and the useEffect hook to set up the observer. Here is an example of how you might do this:

In this example, we used the useRef hook to create a reference to the element we want to observe and the useEffect hook to set up the intersection observer when the component mounts. When the target element intersects with the viewport, the onIntersection callback function is executed, allowing you to respond to the event in your code.

Make sure to clean up the IntersectionObserver you subscribed in useEffect. It's an important optimization of useEffect

Lazy Loading React Components using intersection observer (the easy way)

The react-intersection-observer package is a wrapper around the IntersectionObserver API and makes lazy loading less verbose and easy-peasy. Let's take a look at an example:

In this example, the Video component is a wrapper around HTML's native video element. We only render video when it is in the viewport. It helps boost the performance of the page and core web vitals. You can pass various configurations to the useInView hook as described in the documentation. For example, you can specify a threshold value to control when the component should be considered visible or add a root margin to adjust the boundaries of the viewport.

In conclusion, using the IntersectionObserver API or react-intersection-observer package allows us to implement lazy loading in React components. This can improve the performance of our application by delaying the rendering of heavy components until they are actually needed. By using the useInView hook and monitoring when a component enters the viewport, we can efficiently lazy load our components and improve the user experience.

Find me here:
GitHub  Twitter  LinkedIn  Instagram  Facebook

All Rights Reserved © Huzaima Khan 2023