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-laz-y-component-loader

v1.0.2

Published

Lazy load your components when they appear in the viewport.

Downloads

7

Readme

Table of Contents

About The Project

By combining the power of browser Intersection Observer API and React Lazy API I've created a simple component that will allow you to lazy load your component when it appears in the browser's viewport.

Don't load components that user won't see on the first render, React Laz-y will detect the scroll position for you and inject the correct code when it's actually needed.

Getting Started

To install, follow these few steps.

Prerequisites

Before you start, make sure you use:

  • React >= 16.6.0
  • npm
npm install npm@latest -g

Installation

  • Install with npm
npm i react-laz-y-component-loader

or

  • Install with yarn
yarn add react-laz-y-component-loader

Usage

Basic example

Pass a component imported with React.lazy API as a children.

import React from 'react';
import ReactLazy from 'react-laz-y-component-loader';

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

function App() {
  return (
    <>
      <header style={{ height: '300vh' }}>
        <h1>My awesome title</h1>
      </header>
      <main>
        <ReactLazy>
          <MyComponent />
        </ReactLazy>
      </main>
    </>
  );
};

export default App;

Usage with rootMargin property

Pass the rootMargin props to increase or decrease the area of the root bounding box.

rootMargin props

import React from 'react';
import ReactLazy from 'react-laz-y-component-loader';

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

function App() {
  return (
    <>
      <header style={{ height: '300vh' }}>
        <h1>My awesome title</h1>
      </header>
      <main>
        <ReactLazy rootMargin="100px 0px">
          <MyComponent />
        </ReactLazy>
      </main>
    </>
  );
};

export default App;

Props

| Name | Type | Default | Required | Description | |:------- |:-------------:| -----:|-----:| -----:| | children | ReactElement | undefined | true | Child element loaded with React.lazy API. | | onLoad | function | () => {} | false | Function that will be fired when child element is loaded. | | root | HTMLElement | viewport | false | Parent of the element that will be loaded. | | rootMargin | string | "0px" | false | Defines margin of root bounding box. See more here. | | threshold | number || number[] | 0 | false | Determines how much of the elements wrapper needs to intersect with the bounding box. See more here.| | fallback | ReactElement | <div>Loading...</div> | false | Fallback element that will be rendered while target elements is being loaded. | | wrapperClass | string | "" | false | Custom class for element's wrapper. | | styles | object | {} | false | Custom styles for element's wrapper. |

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Maciek Grzybek - @maciek_g88 - [email protected]

Notes

  • Currently React Laz-y doesn't work with SSR due you React Lazy API limitations.
  • React Lazy API only supports default exports. There's a simple workaround that you can find here.
  • Intersection Observer API doesn't work with Internet Explorer. If you really need to support it, you can use polyfill. Simply import it in your project.