Coder Social home page Coder Social logo

evavic44 / react-lazyload Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 3.0 322 KB

🦥 A tutorial to illustrate how to lazyload images in a react app using react lazy load image component library

Home Page: https://react-lazyload.vercel.app

License: MIT License

HTML 6.85% CSS 14.85% JavaScript 78.29%
lazyload react reactjs asset-optimization images lazyload-image react-lazyload

react-lazyload's Introduction

React Lazyload Images

This tutorial is meant to showcase how to lazyload images in a react application using the NPM library: React Lazy Load Image Component

Default

Default image in react without the lazyload effect Source

import Image from "../images/bird.jpg";
<img className="image" src={Image} width={800} height={550} alt="Fishy" />;

Placeholder

This illustrates the lazyload image component with a placeholder image. To test this, visit the /placeholder page, set throttling in developer tools to Good 2G, and refresh the page to see the effect.

Adding a placeholder image is very simple, simply add placeholderSrc="" attribute with your placeholder image within the quotes to the LazyLoadImage component.

import { LazyLoadImage } from "react-lazy-load-image-component";
import Image from "../images/bird.jpg";
import PlaceholderImage from "../images/placeholder.jpg";

<LazyLoadImage
  src={Image}
  width={800}
  height={500}
  placeholderSrc={PlaceholderImage}
  alt="bird"
/>;

Blur

This illustrates the lazyload image component with a placeholder image and a blur effeect. To test this, visit the /blur p

Combining the placeholder image and blur effect provides the best experience for users. To enable it, add the placeholder image and add and effect set to blur.

You can add effects like:

  • blur: renders a blurred image based on placeholderSrc and transitions to a non-blurred one when the image specified in the src is loaded.
  • black-and-white: renders a black and white image based on placeholderSrc and transitions to a colorful image when the image specified in the src is loaded.
  • opacity: renders a blank space and transitions to full opacity when the image is loaded.

Note: They effects rely on CSS and the corresponding CSS file must be imported:

import { LazyLoadImage } from "react-lazy-load-image-component";
import Image from "../images/bird.jpg";
import PlaceholderImage from "../images/placeholder.jpg";

<LazyLoadImage
  src={Image}
  width={800}
  height={530}
  placeholderSrc={PlaceholderImage}
  effect="blur"
  alt="image"
/>;

Tutorial prepared by Victor Eke

Resources

react-lazyload's People

Contributors

evavic44 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.