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-img-placeholder

v0.1.5

Published

There are no external dependencies, aside for a version of react and react-dom which support hooks.

Downloads

660

Readme

Dependencies

There are no external dependencies, aside for a version of react and react-dom which support hooks.

Getting Started

To include the code locally in ES6, CommonJS, or UMD format, install react-image using npm:

npm i react-img-placeholder

Import

import { Image, useImage } from 'react-img-placeholder'

Documentation

you can use the Image component or create your own custom component with useImage() hook.

Image Component

Default Placeholder Support

  • Every image would show a default placeholder when you use Image component.
  • current default placeholder is block of solid color , color can be changed using prop

Example usage

import { Image } from 'react-img-placeholder';

function LoadImage() {
  return (
    <Image
      src="/me.png"
      alt="Picture of the author"
      width={500}
      height={500}
      placeholderColor="pink" // <- this field is optional
    />
  );
}

Required Props

The <Image /> component requires the following properties.

src

The path or URL to the source image. This is required.

width

The width of the image, in pixels. Must be an integer without a unit.

height

The height of the image, in pixels. Must be an integer without a unit.

Optional Props

placeholder

Takes a React Element which you want to show when image is loading or if src fails

  • type: React.ReactElement
function LoadImage() {
  return (
    <Image
      src="big-size-image.jpg"
      alt="big size"
      height="500"
      width="500"
      placeholder={<Loader />}
    />
  );
}

placeholderSrc

Placeholder image to show when main src image is loading or it fails

Note : you can use other image urls but use local images if possible

import logo from './logo.svg';

function LoadImage() {
  return (
    <Image
      src="/big-size-image.jpg"
      height={200}
      width={200}
      alt="big size logo"
      placeholderSrc={logo}
    />
  );
}

placeholderColor

pass a valid color value when using default placeholder to change the color of loading div

function LoadImage() {
  return (
    <Image
      src="/big-size-image.jpg"
      height={200}
      width={200}
      alt="big size logo"
      placeholderColor="#FFA7C4"
    />
  );
}

ignorePlaceholder

if true component ignores placeholder logic and shows img.

  • default: false
  • type: Boolean
import logo from './logo.svg';

function LoadImage() {
  return (
    <Image
      src="/big-size-image.jpg"
      ignorePlaceholder={true}
      placeholderSrc={logo} // <- this will be ignored
    />
  );
}

Advanced Props

loading

type: eager | lazy

if loading is passed then <Image /> component ignores all placeholder.

onLoad

A callback for when the image src has been loaded.

  • type: Function

onError

A callback for when there was an error loading the image src.

  • type: Function

All img attributes like srcSet, sizes, crossOrigin are supported

useImage() :

The useImage hook allows users to get react-image-placeholder logic in any image component. this hooks returns loading status for source which is passed.

useImage api:

takes object as argument with

  • src (required): source of image for which you want loading states, if src is not passed it would returnpending.
  • ignorePlaceholder (optional): Boolean. if true ignores placeholder logic and always returns 'loaded' status.

returns:

object with following properties

  • status: String : Will be idle fetching is yet to start loading if imaged is being loaded failed if image loading fails loaded if successfully loads image

  • derived booolean from status variable isError, isIdle, isLoading, isLoaded,

Example usage of hook:

import { useImage } from 'react-img-placeholder';

function CustomImage() {
  const { isLoaded, isError, isLoading, isIdle, status } = useImage({
    src: 'https://via.placeholder.com/600/92c952',
  });

  if (isError) return <p>Image not found</p>;

  if (!isLoaded) return <Loader />;

  return <img src="https://via.placeholder.com/600/92c952" />;
}

Acknowedgement

This component is inspired by lot of libraries like react-image, next/image, @chakra-ui/image.