简体   繁体   English

类型“IntersectionObserver”不可分配给类型“null”。 TS2322 [React,intersectionObserver]

[英]Type 'IntersectionObserver' is not assignable to type 'null'. TS2322 [React, intersectionObserver]

Want to create lazy loading with IntersectionObserver, but cannot assign to type null (io.current).想用 IntersectionObserver 创建延迟加载,但不能分配给类型 null (io.current)。 it should assign src link and alt text to attribute when it is in viewport.当它在视口中时,它应该将 src 链接和 alt 文本分配给属性。 Snippet to codesandbox 代码沙盒的片段

 import { FC, useRef, useEffect } from "react"; import { LazyLoaderProps } from '../../types/types'; export const LazyLoader: FC<LazyLoaderProps> = ({ src, alt }) => { const ref = useRef(null); const io = useRef(null); useEffect(() => { if (ref.current) { io.current = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.intersectionRatio > 0.5) { ref.current.src = src; io.current.unobserve(ref.current); } }); }, { threshold: [0, 0.5, 1] } ); io.current.observe(ref.current); } return () => { io.current.unobserve(ref.current); }; }, [ref]); return <img ref={ref} srcalt={alt} />;

Try this:试试这个:

import { FC, useRef, useEffect } from "react";
import { LazyLoaderProps } from '../../types/types';

export const LazyLoader: FC<LazyLoaderProps> = ({ src, alt }) => {
  // Add explicit type, remove initial value since it should always be set by React during rendering
  const ref = useRef<HTMLImageElement>();

  useEffect(() => {
    if (ref.current) {
      const elem = ref.current;

      // Using a local variable is enough for your use case
      const observer = new IntersectionObserver(
        (entries) => {
          entries.forEach((entry) => {
            if (entry.intersectionRatio > 0.5) {
              elem.src = src;
              observer.unobserve(elem);
            }
          });
        },
        {
          threshold: [0, 0.5, 1]
        }
      );

      observer.observe(elem);
      
      return () => {
        observer.unobserve(elem);
      };
    }
  }, [ref]);

  return <img ref={ref} srcalt={alt} />;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “Firebase”类型不能分配给“null”类型。 TS2322 - Type 'Firebase' is not assignable to type 'null'. TS2322 TS2322:类型“计数”不可分配给类型“ReactNode” - TS2322: Type 'Count' is not assignable to type 'ReactNode' TS2322:类型“BillingSummaryDTO[]”不可分配给类型“从不” - TS2322: Type 'BillingSummaryDTO[]' is not assignable to type 'never' 类型“可观察” <boolean | “”> &#39;不可分配给&#39;Observable类型 <boolean> &#39;TS2322 - Type 'Observable<boolean | “”>' is not assignable to type 'Observable<boolean>' TS2322 TypeScript TS2322:类型'typeof Foo'不能分配给'IFoo'类型 - TypeScript TS2322: Type 'typeof Foo' is not assignable to type 'IFoo' TS2322:键入&#39;{ready:false; 会话:null; }”不可分配为“ Readonly <S & withAuthState>”</s>类型 - TS2322: Type '{ ready: false; session: null; }' is not assignable to type 'Readonly<S & withAuthState>' React.createElement问题:TS2322类型T不可分配给类型ReactElement &lt;{}&gt; - React.createElement issue: TS2322 Type T is not assignable to type ReactElement<{}> Typescript - React 错误 TS2322:类型 'AppState | undefined' 不能分配给类型 'ICards | 不明确的' - Typescript - React Error TS2322: Type 'AppState | undefined' is not assignable to type 'ICards | undefined' React TypeScript 错误:TS2322 - 类型''不可分配给类型'IntrinsicAttributes &amp; String' - React TypeScript Error : TS2322 - Type '' is not assignable to type 'IntrinsicAttributes & String' 角度:错误TS2322:类型&#39;Observable &lt;{}&gt;&#39;不可分配...使用share()运算符 - Angular: error TS2322: Type 'Observable<{}>' is not assignable … with share() operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM