twobin / react-lazyload

Lazy load your component, image or anything matters the performance.
MIT License
5.88k stars 488 forks source link

SSR support #377

Open Saeeed-B opened 2 years ago

Saeeed-B commented 2 years ago

@ameerthehacker This package does not appear to support ssr. Photos are not displayed in the viewport.

ameerthehacker commented 2 years ago

No more features are actively developed but I'm happy to accept a PR if you wish to contribute

prmichaelsen commented 2 years ago

@Saeeed-B

  /**
   * https://github.com/alex-cory/use-ssr
   * https://github.com/alex-cory/use-ssr/blob/master/useSSR.ts
   */
  const isBrowser: boolean = !!(
    typeof window !== 'undefined' &&
    window.document &&
    window.document.createElement
  );

  const Lazy = ({ children }) => {
    if (lazy && isBrowser) {
      return (
        <LazyLoad
          className={classes.avatar}
          once
          placeholder={<LoadingSpinner />}
        >
          {{children}}
        </LazyLoad>
      );
    } else {
      return children;
    }
  }