Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placeholder images #82

Closed
SrSandeepKumar opened this issue Dec 16, 2016 · 7 comments
Closed

Placeholder images #82

SrSandeepKumar opened this issue Dec 16, 2016 · 7 comments

Comments

@SrSandeepKumar
Copy link

SrSandeepKumar commented Dec 16, 2016

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
Copy link

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
Copy link
Owner

loktar00 commented Feb 15, 2017

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
Copy link

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

@danielbush
Copy link

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

@isabellachen
Copy link

@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
Copy link

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants