TS-React:useRef 使用

不能给 useRef 的 current 属性赋值,提示一下错误信息:
Cannot assign to ‘current’ because it is a read-only property.(不能给current属性复制,因为它是一个只读属性。)

代码如下所示:

let cameraMediaStream = useRef<MediaStream>(null);

/**
 * 打开摄像头
 **/
const openCamera = async (cameraId = curCameraId) => {
   
    try {
   
      // 关闭已打开的摄像头
      if (cameraMediaStream) await closeCamera();
      // Cannot assign to 'current' because it is a read-only property.
      cameraMediaStream.current = await ImageCapture.openCamera({
    cameraId, video: videoRef.current, width, height });
    }
    catch (err: any) {
   
    	// 错误信息提示...
    }
}

解决方案:将 current 属性变为非只读属性,useRef 的泛型参数中增加【| null】即可把 current 属性变为非只读属性。

let cameraMediaStream = useRef<MediaStream | null>(null);

相关推荐

  1. TS-React:useRef 使用

    2024-01-23 06:08:06       31 阅读
  2. React 中使用 TS

    2024-01-23 06:08:06       14 阅读
  3. TS-Antd:组件使用记录

    2024-01-23 06:08:06       32 阅读
  4. Vue3 ts使用echarts

    2024-01-23 06:08:06       17 阅读
  5. [TS面试]TS使用Union Types时注意事项?

    2024-01-23 06:08:06       19 阅读
  6. react native使用TS实现路由

    2024-01-23 06:08:06       17 阅读
  7. 在vue项目中使用TS

    2024-01-23 06:08:06       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-23 06:08:06       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-23 06:08:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-23 06:08:06       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-23 06:08:06       20 阅读

热门阅读

  1. Mongodb 控制查询返回字段

    2024-01-23 06:08:06       34 阅读
  2. RPC教程 4.超时处理机制

    2024-01-23 06:08:06       27 阅读
  3. Redis通过dump.rdb恢复数据

    2024-01-23 06:08:06       36 阅读
  4. MySQL数据备份

    2024-01-23 06:08:06       32 阅读
  5. 使用docker以容器方式安装redis

    2024-01-23 06:08:06       30 阅读
  6. 【PostgreSQL】创建枚举类型和修改枚举值

    2024-01-23 06:08:06       35 阅读
  7. 使用github作为docker镜像存储仓库

    2024-01-23 06:08:06       34 阅读