React报错之Cannot assign to 'current' because it is a read-only property
正文从这开始~
总览
当我们用一个null值初始化一个ref,但在其类型中不包括null时,就会发生"Cannot assign to 'current' because it is a read-only property"错误。为了解决该错误,请在ref的类型中包含null。比如说,const ref = useRef<string | null>(null) 。
这里有个例子来展示错误是如何发生的。
// App.tsx
import {useEffect, useRef} from 'react';
const App = () => {
const ref = useRef<string>(null);
useEffect(() => {
// ️ Error: Cannot assign to 'current' because it is a read-only property.ts(2540)
ref.current = 'hello';
}, []);
return (
<div>
<h2>hello world</h2>
</div>
);
};
export default App;
上面例子的问题在于,当我们将null作为初始值传递到useRef钩子中时,并且我们传递给钩子的泛型中没有包括null类型,我们创建了一个不可变的ref对象。
正确的泛型
为了解决该错误,我们必须在传递给钩子的泛型中包括null类型。
// App.tsx
import {useEffect, useRef} from 'react';
const App = () => {
// ️ include null in the ref's type
const ref = useRef<string | null>(null);
useEffect(() => {
ref.current = 'hello';
}, []);
return (
<div>
<h2>hello world</h2>
</div>
);
};
export default App;
在ref的类型中,我们使用联合类型来包括null类型,这使它成为可变ref对象。
现在这个例子中
ref的类型是字符串或null,它的current属性可以赋值为这两种类型中的任意一种的值。
DOM元素
如果你的引用指向一个DOM元素,情况也是一样的。如果你需要改变ref的current属性的值,你必须将钩子的类型定为 const ref = useRef<HTMLElement | null>(null)。
注意,如果你不直接赋值给它的current属性,你不必在 ref 的类型中包含 null。
// App.tsx
import {useEffect, useRef} from 'react';
const App = () => {
const ref = useRef<HTMLInputElement>(null);
useEffect(() => {
ref.current?.focus();
}, []);
return (
<div>
<input ref={ref} type="text" defaultValue="" />
</div>
);
};
export default App;
上述例子中的ref是用来聚焦input元素的。因为没有对其.current属性进行赋值,所以没有必要在其类型中包含null。
React报错之Cannot assign to 'current' because it is a read-only property的更多相关文章
- react 报错的堆栈处理
react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...
- vue运行报错error:Cannot assign to read only property 'exports' of object '#<Object>'
用weex做项目的时候,npm start 之后一直报错error:Cannot assign to read only property 'exports' of object '#<Obje ...
- Spring+Hibernate4 Junit 报错No Session found for current thread
论坛上有另外一篇更全面的帖子,jinnianshilongnian写的:http://www.iteye.com/topic/1120924 本文的环境是: spring-framework-3.1 ...
- React报错 :browserHistory doesn't exist in react-router
由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...
- react报错 TypeError: Cannot read property 'setState' of undefined
代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...
- React报错之Cannot find name
正文从这开始~ .tsx扩展名 为了在React TypeScript中解决Cannot find name报错,我们需要在使用JSX文件时使用.tsx扩展名,在你的tsconfig.json文件中把 ...
- React报错之Cannot find namespace context
正文从这开始~ 总览 在React中,为了解决"Cannot find namespace context"错误,在你使用JSX的文件中使用.tsx扩展名,在你的tsconfig. ...
- React报错之Expected `onClick` listener to be a function
正文从这开始~ 总览 当我们为元素的onClick属性传递一个值,但是该值却不是函数时,会产生"Expected onClick listener to be a function" ...
- React报错:Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix,
今天在开发时报了以下错误,记录一下 我们不能在组件销毁后设置state,防止出现内存泄漏的情况 出现原因直接告诉你了,组件都被销毁了,还设置个锤子的state啊 解决方案: 利用生命周期钩子函数:co ...
随机推荐
- Springmvc基础及应用
SpringMVC简介和环境搭建 SpringMVC简介 Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一.在Spring3.0 后全面超越 S ...
- einsum函数介绍-张量常用操作
einsum函数说明 pytorch文档说明:\(torch.einsum(equation, **operands)\) 使用基于爱因斯坦求和约定的符号,将输入operands的元素沿指定的维数求和 ...
- [BZOJ5449] 序列
题目链接:序列 Description 给定一个\(1\)~\(n\)的排列x,每次你可以将 \(x_1, x_2, ..., x_i\) 翻转. 你需要求出将序列变为升序的最小操作次数. 多组数据. ...
- 白嫖Azure与体验GoLand远程开发
前言 近期因为有本地开发远程使用Linux编译部署的需求,而虚拟机的性能实在是不敢恭维,WSL的坑之前也踩过(没有systemd等),故考虑使用SSH连接云服务器开发. 目前VSCode提出了Remo ...
- 给小白的 PG 容器化部署教程(下)
作者:王志斌 编辑:钟华龙 本文来自社区小伙伴 王志斌 的投稿.从小白的角度,带你一步步实现将 RadonDB PostgreSQL 集群部署到 Kubernetes 上.文章分为上下两部分,< ...
- 可变参数——JavaSE基础
可变参数 方法声明中,在指定参数类型后加一个省略号...即可声明可变参数 可变参数必须是参数列表的最后一个参数 声明 public void test(int... i){ System.out.pr ...
- EasyExcel-合并单元格
pom版本 <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</ ...
- c++ 平衡树
平衡树的性质 它其实就是一个 BST(Binary Search Tree 二叉搜索树). 当然,不同的平衡树会有自己的特性 BST 的性质 只有一个:任意一个节点的左子树的所有节点都比它的优先级高, ...
- 七牛云创建存储空间并绑定自定义域名-https协议
七牛云创建存储空间并绑定自定义域名-https协议 一.准备 0.绑定自定义域名的前提:你起码拥有过一个备案过的域名[一级域名] 1.在七牛云创建一个存储空间 2.存储空间绑定自定义域名(cdn加速) ...
- 关于webstorm打开HTML文件出现404错误的情况
第一种情况是你的端口号错误.你可以到设置里面找到调试器(第四个可以展开的按钮里面),找到端口号,把端口号改成8080(默认),再勾选旁边的按钮(可以接受外部链接). 你的文件命名方式不对,最好的文件名 ...