正文从这开始~

类型守卫

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

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

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>Click</button>
</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>Click</button>
</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>Click</button>
</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>Click</button>
</div>
);
}

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

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

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

总结

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

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

React报错之Object is possibly null的更多相关文章

  1. oracle创建包后执行报错:object omgmig.test_package is invalid.

    今天学习了一下oracle的包的写法,然后碰到这么个问题.包声明和包主体都正确,但是就是执行报错:object omgmig.test_package is invalid. 这是会报错的sql,看起 ...

  2. react 报错的堆栈处理

    react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...

  3. python报错'str' object is not callable

    >>> x=1.235 >>> int(x) 1 >>> str="fsgavfdbafdbntsbgbt" >> ...

  4. Spring Boot整合Swagger报错:"this.condition" is null

    前段时间看到群里有吐槽swagger整合问题,当时没仔细看,总以为是姿势不对. 这两天正好自己升级Spring Boot版本,然后突然出现了这样的一个错误: Caused by: java.lang. ...

  5. React报错之Cannot find namespace context

    正文从这开始~ 总览 在React中,为了解决"Cannot find namespace context"错误,在你使用JSX的文件中使用.tsx扩展名,在你的tsconfig. ...

  6. tomcat启动时候报错Can't convert argument: null

    一.启动报错: 为了避免导入的项目重名,我先修改了前一个项目的名称. 重新启动该项目至tomcat,报错:java.lang.IllegalArgumentException: Cant conver ...

  7. 报错:org.hibernate.AssertionFailure: null id in com.tt.hibernate.entities.News entry (don't flush the Session after an exception occurs)

    在使用hibernate创建数据库的表格时,出现了如下报错: 十二月 28, 2016 10:17:02 上午 org.hibernate.tool.hbm2ddl.SchemaExport perf ...

  8. [terry笔记]IMPDP报错ORA-39083 Object type TYPE failed to create ORA-02304

    今天在使用impdp导入的时候(同一数据库中转换schema),遇到了 ORA-39083: Object type TYPE failed to create with error: ORA-023 ...

  9. 【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe' - IronPython 2.7

    最近在开发微信公众号,有一个自定义消息回复的需求 比如用户:麻烦帮我查询一下北京的天气? 系统回复:北京天气,晴,-℃... 这时候需要根据关键字[北京][天气],分词匹配需要执行的操作,然后去调用天 ...

随机推荐

  1. redis 2 主从和哨兵

    主从: 概念:将一台redis服务器数据复制到其他redis服务器,前者是master,后者是slave.数据复制是单向,从主节点复制到从节点.master以写为主,slave以读为主一个zhu主节点 ...

  2. Apache ShenYu:分析、实现一个 Node.js 语言的 HTTP 服务注册客户端(HTTP Registry)

    这块没空写文章了,先贴出实现代码吧 yuque.com/myesn

  3. Nvidia Triton使用教程:从青铜到王者

    1 相关预备知识 模型:包含了大量参数的一个网络(参数+结构),体积10MB-10GB不等 模型格式:相同的模型可以有不同的存储格式(可类比音视频文件),目前主流有torch.tf.onnx和trt, ...

  4. 认识并安装WSL

    认识并安装WSL(基于Windows的Linux子系统) 什么是WSL WSL(Windows Subsystem for Linux),这是在windows平台运行的linux子系统.也就是说可是不 ...

  5. VMWare中CentOS安装VM-Tools

    查看CD-ROM驱动器的设备信息 可以通过下面几个命令来查看 dmesg命令 dmesg | egrep -i --color 'cdrom|dvd|cd/rw|writer' /proc/sys/d ...

  6. Seata源码分析(一). AT模式底层实现

    目录 GlobalTransactionScanner 继承AbstractAutoProxyCreator 实现InitializingBean接口 写在最后 以AT为例,我们使用Seata时只需要 ...

  7. 论文阅读 Dynamic Network Embedding by Modeling Triadic Closure Process

    3 Dynamic Network Embedding by Modeling Triadic Closure Process link:https://scholar.google.com.sg/s ...

  8. Python Selenium库

    Selenium库 自动化测试工具,支持多种游览器 爬虫中主要用来解决JavaScript渲染的问题 安装Selenium pip3 install selenium 安装游览器驱动 下载驱动地址:h ...

  9. Cabloy-CMS中区块的开发与效果

    关于区块 Cabloy-CMS引入了区块的概念,通过区块可以快速向文章添加各种类型的内容,比如:插入一个地图子页面.插入一首音乐,等等 Cabloy-CMS中的区块可以类比于Wordpress古腾堡编 ...

  10. bitmap技术解析:redis与roaringBitmap

    bitmap的表象意义是,使用一个01标识位来表示是否的状态,可以达到节省空间和高效判定的效果.在我们的实际工作中,也有着许多的应用场景,相信了解bitmap定会给你带来一些额外的收获. 1. bit ...