loktar00 / react-lazy-load

React component that renders children elements when they enter the viewport.
MIT License
979 stars 166 forks source link

Placeholder images #82

Closed SrSandeepKumar closed 2 years ago

SrSandeepKumar commented 7 years ago

Is their a way we can keep a placeholder(text / image / gif) before the element appears on DOM Ex: We have few set of images in list and we have to display a loader before the image appears.

rubencodes commented 7 years ago

Not sure if there's an official solution, but in my project we literally have text "under" the image element, such that when it's not loaded you can see the text :)

loktar00 commented 7 years ago

Why not just wrap a loader in it as well?

<LazyLoad>
        <myloader isloaded={something}>
              <thing to be loaded/>
        </myLoader>
</LazyLoad>

The loader component would display something else when not loaded something like the following,

render() {
        let loadingElement = null;
        if (!this.props.loaded) {
            loadingElement = <whatever image or text loading indicator maybe a spinner/>;
        }
        return (
            <div>
                {loadingElement}
                {this.props.children}
            </div>
        );
    }

Regardless this doesn't seem something that the LazyLoad component should handle. Unless people disagree. There is a PR to have a placeholder prop as well. Interested in thoughts.

ParadeTo commented 7 years ago

Look! Not just me who want to have this feature. ^_^

danielbush commented 7 years ago

81 - just for reference as there is some discussion there too.

isabellachen commented 5 years ago

@loktar00 In your example, how can the LazyLoad component tell the wrapper when the image is loaded and thus change the this.props.loaded from false to true?

gleb-lobastov commented 3 years ago

My solution, if somebody still interested

import React, { useState, useCallback } from 'react';
import LazyLoad from 'react-lazy-load';

export default function LazyWithPlaceholder({
  placeholder,
  children,
  ...forwardingProps
}) {
  const [loaded, setLoaded] = useState(false);
  const handleVisible = useCallback(() => setLoaded(true), []);

  return (
    <>
      {!loaded && placeholder}
      <LazyLoad {...forwardingProps} onContentVisible={handleVisible}>
        {children}
      </LazyLoad>
    </>
  );
}
loktar00 commented 2 years ago

I'll add this as an example for the examples area in the branch I'm working it, https://github.com/loktar00/react-lazy-load/tree/feature/update-to-18