• React报错之Object is possibly null


    正文从这开始~

    类型守卫

    使用类型守卫来解决React中useRef钩子“Object is possibly null”的错误。比如说,if (inputRef.current) {} 。一旦null被排除在ref的类型之外,我们就能够访问ref上的属性。

    useref-object-is-possibly-null.webp

    下面是一个错误如何发生的示例。

    import {useEffect, useRef} from 'react';
    
    export default function App() {
      const inputRef = useRef<HTMLInputElement>(null);
    
      useEffect(() => {
        // ⛔️ Object is possibly 'null'.ts(2531)
        inputRef.current.focus();
      }, []);
    
      return (
        <div>
          <input ref={inputRef} type="text" id="message" />
          <button>Clickbutton>
        div>
      );
    }
    

    代码片段中的问题是,TypeScript不能确保我们将一个元素或者一个值赋值给ref,所以它的current属性可能为null

    为了解决这个错误,在访问ref类型上的属性之前,我们必须使用类型守卫来从其类型中排除null

    import {useEffect, useRef} from 'react';
    
    export default function App() {
      const inputRef = useRef<HTMLInputElement>(null);
    
      useEffect(() => {
        // 👉️ ref could be null here
        if (inputRef.current != null) {
          // 👉️ TypeScript knows that ref is not null here
          inputRef.current.focus();
        }
      }, []);
    
      return (
        <div>
          <input ref={inputRef} type="text" id="message" />
          <button>Clickbutton>
        div>
      );
    }
    

    我们使用简单的if语句作为类型守卫,来确保ref上的current属性不存储null。当程序进入到if代码块中,TypeScript就会知道ref对象上的current属性就不会存储null

    确保在useRef钩子上使用泛型,正确的类型声明ref上的current属性。

    注意,我们传递了一个泛型来将ref的值类型声明为HTMLInputElement

    一些常用的类型有:HTMLInputElementHTMLButtonElementHTMLAnchorElementHTMLImageElementHTMLTextAreaElementHTMLSelectElement 等等。

    如果你在ref中存储了不同的值,请确保将特定类型传递给useRef钩子的泛型,例如const ref = useRef<{name: string}>(null);

    如果ref上的current属性存储了null,我们也可以使用可选链?. 操作符进行短路运算。

    import {useEffect, useRef} from 'react';
    
    export default function App() {
      const inputRef = useRef<HTMLInputElement>(null);
    
      useEffect(() => {
        // 👇️ optional chaining (?.)
        inputRef.current?.focus();
      }, []);
    
      return (
        <div>
          <input ref={inputRef} type="text" id="message" />
          {/* Cannot find name 'button'.ts(2304) */}
          <button>Clickbutton>
        div>
      );
    }
    

    如果引用是空值(null或者undefined),可选链?.操作符会进行短路运算,而不会抛出错误。换句话说,如果ref上的current属性存储了null,操作符会短路运算从而返回undefined。而不会在undefined上尝试调用focus方法,导致一个运行时错误。

    非空断言

    另一种解决方案是使用非空断言!操作符。

    import {useEffect, useRef} from 'react';
    
    export default function App() {
      const inputRef = useRef<HTMLInputElement>(null);
    
      useEffect(() => {
        // 👇️ using non-null (!) assertion
        inputRef.current!.focus();
      }, []);
    
      return (
        <div>
          <input ref={inputRef} type="text" id="message" />
          {/* Cannot find name 'button'.ts(2304) */}
          <button>Clickbutton>
        div>
      );
    }
    

    在TypeScript中,感叹号标记被称为非空断言操作符。被用来从类型中移除nullundefined ,而不用进行任何显式的类型检查。

    当我们使用非空断言时,基本上我们就是在告诉TS,ref对象上的current属性不会存储null或者undefined

    请注意,这种方法不是类型安全的,因为TypeScript不执行任何检查以确保属性不是空的。

    总结

    造成 "Object is possibly null"的错误是因为useRef()钩子可以传递一个初始值作为参数,而我们传递null作为初始值。该钩子返回一个可变的ref对象,其.current属性被初始化为所传递的参数。

    当传递ref prop给一个元素时,比如 ,React将ref对象的.current属性设置为相应的DOM节点,但TypeScript无法确定我们是否会将ref设置为DOM元素,或在我们的代码中稍后设置其值。

  • 相关阅读:
    【C语言从入门到放弃 3】函数、枚举、指针、函数指针和回调函数详解
    设备巡检维修报备小程序开发制作功能介绍
    【数据结构】—— 链表/队列/栈/单调栈/单调队列
    安装开源的apache的依赖库apr
    C/C++不同编译器对数组为0和void的处理
    时间序列的数据分析(四):STL分解
    头戴式耳机什么牌子最好?头戴式耳机推荐性价比高
    My Seventy-ninth Page - 完全平方数 - By Nicolas
    【Mybatis源码】源码分析
    解决Docker安装MySQL不区分大小写问题
  • 原文地址:https://www.cnblogs.com/chuckQu/p/16533611.html