Socket
Book a DemoInstallSign in
Socket

interserver-react

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

interserver-react

IntersectionObserver simplified

0.1.2
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

Interserver React

npm package npm bundle size NPM GitHub last commit

IntersectionObserver simplified

Checkout the main interserver package.

Features

  • Tiny (~1kb minified)
  • TypeScript ready

Installation

With yarn:

yarn add interserver-react

With npm:

npm install --save interserver-react

Usage

The useInterserver function takes the same options as the interserver function (top, right, bottom, left and once).

import React from "react";
import useInterserver from "interserver-react";

const MyComponent = () => {
  const { isIntersecting, setRef } = useIntersector();

  return (
    <div ref={setRef}>
      {isIntersecting ? "Now you see me!" : "Oh, the darkness..."}
    </div>
  );
};

Example

You can build a LazyImage component, that only requests an image, when it is approaching the viewport:

// LazyImage.jsx
import React from "react";
import useInterserver from "interserver-react";

const LazyImage = ({ alt, src, srcSet, ...props }) => {
  const { isIntersecting, setRef } = useInterserver({
    once: true,
    top: 50,
    right: 50,
    bottom: 50,
    left: 50,
  });
  const imgSrc = isIntersecting ? src : undefined;
  const imgSrcSet = isIntersecting ? srcSet : undefined;
  return (
    <img
      {...props}
      src={imgSrc}
      srcSet={imgSrcSet}
      alt={alt}
      ref={setRef}
    />
  );
};

LazyImage.propTypes = propTypes;
LazyImage.defaultProps = defaultProps;

export default LazyImage;

License

MIT

Keywords

IntersectionObserver

FAQs

Package last updated on 14 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.