相关疑难解决方法(0)

useRef 的类型(如果与 setInterval、react-typescript 一起使用)

我正在 next.js 应用程序中制作一个简单的动画。

let flipInterval = useRef();



const startAnimation = () => {
    flipInterval.current = setInterval(() => {
      setIsFlipping((prevFlipping) => !prevFlipping);
    }, 10000);
  };
Run Code Online (Sandbox Code Playgroud)

因为flipInterval.current我收到“类型‘超时’不能分配给类型‘未定义’”。所以我检查了如何使用超时类型,我看到人们正在使用,但那不起作用。

 let flipInterval = useRef<typeof window.settimeout>();
Run Code Online (Sandbox Code Playgroud)

我也通过了号码useRef<number>()我收到“类型‘超时’不能分配给类型‘数字’”

这也不起作用

  let flipInterval = useRef<typeof window.setInterval>();
Run Code Online (Sandbox Code Playgroud)

animation settimeout typescript reactjs next.js

26
推荐指数
3
解决办法
2万
查看次数

TS2339:属性“getBoundingClientRect”在“从不”类型上不存在

我知道可选链接应该足够了,但我在这里尝试满足 TypeScript 有点过火:

const ref = useRef()

    if (ref !== undefined) {
        if(ref.hasOwnProperty('current')) {
            if (ref.current !== undefined &&  ref.current !== null)
             console.log(ref?.current?.getBoundingClientRect())
        }
    }
Run Code Online (Sandbox Code Playgroud)

错误:

TS2339: Property 'getBoundingClientRect' does not exist on type 'never'.
Run Code Online (Sandbox Code Playgroud)

我想念我不打字的日子......除了 @ts-ignore

typescript reactjs

4
推荐指数
1
解决办法
753
查看次数

标签 统计

reactjs ×2

typescript ×2

animation ×1

next.js ×1

settimeout ×1