小编Sho*_*har的帖子

类型“MutableRefObject<undefined>”不可分配给类型“LegacyRef<HTMLDivElement> | 不明确的'

我设置了一个自定义挂钩,它获取一个ref对象并观察它:

import { useState, useEffect, MutableRefObject } from "react";

const UseOnScreen = (ref: MutableRefObject<undefined>) => {
  const [isIntersecting, setIntersecting] = useState(false);
  useEffect(() => {
    const observer = new IntersectionObserver(([entry]) =>
      setIntersecting(entry.isIntersecting)
    );
    if (ref.current) {
      observer.observe(ref.current);
    }
  });
  return isIntersecting;
};
export default UseOnScreen;
Run Code Online (Sandbox Code Playgroud)

接下来,在我的页面组件中,我声明了一个refusing useRef()

import { useRef } from "react";
import UseOnScreen from "../hooks/UseOnScreen";
import About from "./about";

export default function Home() {
  const aboutRef = useRef();
  const aboutRefValue = UseOnScreen(aboutRef);

  return ( …
Run Code Online (Sandbox Code Playgroud)

javascript typescript reactjs next.js

6
推荐指数
1
解决办法
4865
查看次数

为什么 snapshot.data.length 出现错误?

我正在尝试从 API 解析数据。为此,我使用 FutureBuilder 在 ListView 中列出所有解析的数据。

我已经执行了无效检查,snapshot.data但我继续在段中收到此错误snapshot.data.length,它说,The property 'length' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!').

我在本节中也有类似的错误snapshot.data[i],其中写着The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').

这是我的代码部分:

 body: Container(
        child: FutureBuilder(
          future: getData('hello'), …
Run Code Online (Sandbox Code Playgroud)

dart flutter flutter-futurebuilder

1
推荐指数
2
解决办法
3611
查看次数