loading...

🌠 react-shimmer: a Better way to handle async images in React!

gokcan profile image Gokcan Degirmenci ・1 min read

The Problem 😐

I needed a custom Image component to asynchronously load images in React. After experimenting with possible ideas, I wanted to apply Facebook's original placeholder effect(e.g. Shimmer) in my custom Image component. The challenge was: it also must work nicely with different image sizes and ratios.

Introducing the solution 🧠

react-shimmer is a powerful, highly customizable React <img> component that simulates a shimmer 🌠 effect while the image is loading. (with zero dependencies!).

  • Written entirely in next-gen JS.
  • Exactly zero-dependency! Only a few KBs.
  • Animation duration, delay and color options are fine-tunable.
  • Better error handling compared to default <img>.
  • Same feel of the shimmer effect on different aspect ratios and sizes.
  • (P.S. Looking for new contributors, feel free to ping me!)

GitHub logo gokcan / react-shimmer

🌠 Async loading, performant Image component for React.js

Logo

A powerful, customisable, Suspense-like <img> component that (optionally) simulates a shimmer effect while loading. (with zero dependencies!).

NPM JavaScript Style Guide Github Actions CI Status Maintainability

Header

Live Demo

Install

npm i react-shimmer

or

yarn add react-shimmer

Usage

import React from 'react'
import Image, { Shimmer } from 'react-shimmer'
function App() {
  return (
    <div&gt
      <Image
        src='https://source.unsplash.com/random/800x600'
        fallback={<Shimmer width={800} height={600} />}
      /&gt
    </div>
  )
}

or you can use your custom React component as a fallback:

import React from 'react'
import Image from 'react-shimmer'

import Spinner from './Spinner'

function App(props) {
  return (
    <div>
      <Image
        src="https://example.com/test.jpg"
        fallback={<Spinner />}
      />
    </div>
  )
}

Properties

Property Type Required Default value
…

react-shimmer

  • You may also reach out to me on Twitter.

Posted on by:

Discussion

markdown guide
 

I already love it! this has been an issue for me for way too long Thanks!