npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-dom-lazyload-component

v6.0.0

Published

React Lazy Load Component for your Website Performance🚀

Downloads

44

Readme

react-dom-lazyload-component

npm version

Lazyload your Components, Images or anything else. You can improve performance score lik Core Web Vitals.

Features

  • ⚡️ Optimized performance: Reuses Intersection Observer instances where possible
  • 💥 Minimum bundle: Around ~1kB
  • 🛠 TypeScript: It'll fit right into your existing TypeScript project
  • 💡 Easy to understand: You don't have to know about complex Intersection Observer API
  • 😽 React 18: Optimized using Suspense and startTransition

Install

npm i react-dom-lazyload-component
yarn add react-dom-lazyload-component
pnpm add react-dom-lazyload-component

Usage

import { lazy } from 'react';
import LazyLoad, { useLazyLoad } from 'react-dom-lazyload-component';
import { Header, Main, Loading } from './MyComponents';

const Footer = React.lazy(() => import('./Footer'))

const App = () => (
    <>
        <Header />
        <Main />
        {/* Footer don't needed to be rendered first. */}
        {/* In this case, it will have been rendered in browser viewport. */}
        {/* This will optimize Core Web Vitals */}
        <LazyLoad
          as='footer'
          fallback={<Loading />}
          suspense
        >
          <Footer />
        </LazyLoad>
    </>
)

// You can also use hooks.
const App = () => {
    const { ref, isVisible } = useLazyLoad();

    return (
        <>
            <Header />
            <Main />
            <footer ref={ref}>{isVisible ? 'footer' : <Loading />}</footer>
        </>
    )
}

Props

useLazyLoad

| Name | Required | Type | Default | Description | |--------------|----------|--------------------------------|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | rootId | No | string | - | The id of element which is IntersectionObserver's target. If rootId is not specified, then the bounds of the actual document viewport are used. This prop wraps IntersectionObserver.root because of performance. | | direction | No | 'vertical' | 'horizontal' | 'vertical' | Direction which user will scroll. | | margin | No | string | '0px' | Margin around the root element. For examples, if direction is vertical and margin is 200px, IntersectionObserver.thresholds is 200px 0px. | | forceVisible | No | boolean | false | You can forces the component to display regardless of whether the element is visible in the viewport. | | once | No | boolean | true | You can control whether the element in the viewport is shown at once or not. |

The return value is ref and isVisible. You can use ref to attach to the element you want to observe, and isVisible to determine if the element is visible in the viewport.

LazyLoad

You can specify the following props in addition to the useLazyLoad props.

| Name | Required | Type | Default | Description | |--------------|----------|--------------------------------|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | children | Yes | ReactNode | - | Component is rendered when it is in the viewport. Automatically enable React.Suspense if you use React.lazy . | | fallback | No | ReactNode | - | Component is rendered when it is not in the viewport. | | as | No | string | div | You can specify tag name to LazyLoad component. | | suspense | No | boolean | false | You can use React.Suspense . | | onVisible | No | () => void | - | Callback function called when the component has been visible. |

LazyLoad also can be received props like className, style and id.

Browser Support

Please see Intersection Observer API.

Demo

https://hiroki0525.github.io/react-dom-lazyload-component/