Lädt...


🔧 图片懒加载组件


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

1、主要使用IntersectionObserver API

自定义useIntersectionObserver Hook

import { useEffect, useRef, useState } from 'react';

const useIntersectionObserver = (callback, options) => {
  const [entry, setEntry] = useState(null);
  const observer = useRef(null);

  useEffect(() => {
    if (observer.current) observer.current.disconnect();

    observer.current = new IntersectionObserver(([entry]) => {
      setEntry(entry);
      if (entry.isIntersecting) {
        callback(entry);
      }
    }, options);

    return () => {
      if (observer.current) observer.current.disconnect();
    };
  }, [callback, options]);

  return { setTarget: (node) => node && observer.current.observe(node) };
};

创建 LazyImage 组件

import React, { useRef, useState } from 'react';

const LazyImage = ({ src, alt, placeholder }) => {
  const [isLoaded, setIsLoaded] = useState(false);
  const imgRef = useRef();

  const onIntersect = () => {
    if (imgRef.current) {
      imgRef.current.src = src;
      imgRef.current.onload = () => setIsLoaded(true);
    }
  };

  const { setTarget } = useIntersectionObserver(onIntersect, {
    root: null,
    rootMargin: '0px',
    threshold: 0.1,
  });

  return (
    <div ref={setTarget} style={{ minHeight: '200px', minWidth: '200px' }}>
      <img
        ref={imgRef}
        src={isLoaded ? src : placeholder}
        alt={alt}
        style={{ opacity: isLoaded ? 1 : 0.5, transition: 'opacity 0.5s' }}
      />
    </div>
  );
};

export default LazyImage;

使用 LazyImage 组件

import React from 'react';
import LazyImage from './LazyImage';

const App = () => {
  return (
    <div>
      <h1>Lazy Load Images Example</h1>
      <LazyImage
        src="https://example.com/image.jpg"
        alt="Example Image"
        placeholder="https://via.placeholder.com/200"
      />
      {/* 可以添加更多的 LazyImage 组件 */}
    </div>
  );
};

export default App;
...

matomo