解决React Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
问题
当我使用如下方式调用组件子组件UploadModal并且绑定Ref时React报错“Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?”;
const UploadModalRef = useRef(null);
const openUploadModal = () => {
UploadModalRef.current.open();
}
// ...
<UploadModal ref={UploadModalRef} />
然而当我按照报错提示使用forwardRef包裹子组件UploadModal后依然报错“TypeError: Cannot read properties of null (reading 'open')”无法访问子组件的方法。
解决:
通过useImperativeHandle暴露子组件方法
import { forwardRef, useState, useImperativeHandle } from "react";
export default forwardRef(function UploadModal(props, ref) {
const [Visible, setVisible] = useState(false);
const
open = () => setVisible(true),
;
useImperativeHandle(ref, () => {
return {
open,
}
})
return (
//...
)
})
useImperativeHandle
是 React 中的一个 Hook,它能让你自定义由 ref 暴露出来的句柄。https://zh-hans.react.dev/reference/react/useImperativeHandle
解决React Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?的更多相关文章
- React报错之Function components cannot have string refs
总览 当我们在一个函数组件中使用一个字符串作为ref时,会产生"Function components cannot have string refs"错误.为了解决该错误,使用u ...
- [React] Use the React Effect Hook in Function Components
Similar to the State Hook, the Effect Hook is “first-class” in React and handy for performing side e ...
- [React] 09 - Tutorial: components
jsx变为js的过程:http://babeljs.io/repl/ youtube: https://www.youtube.com/channel/UCA-Jkgr40A9kl5vsIqg-BIg ...
- Warning: Function created with compilation errors.
SQL> create or replace function 2 remove_constants(p_query in varchar2) return varchar2 3 as 4 l_ ...
- [React] Write Compound Components
Compound component gives more rendering control to the user. The functionality of the component stay ...
- 【转】Facebook React 和 Web Components(Polymer)对比优势和劣势
原文转自:http://segmentfault.com/blog/nightire/1190000000753400 译者前言 这是一篇来自 StackOverflow 的问答,提问的人认为 Rea ...
- Facebook React 和 Web Components(Polymer)对比优势和劣势
目录结构 译者前言 Native vs. Compiled 原生语言对决预编译语言 Internal vs. External DSLs 内部与外部 DSLs 的对决 Types of DSLs - ...
- 解决 dpkg: warning: files list file for package 'x' missing 问题
参考: dpkg: warning: files list file for package 'x' missing 解决 dpkg: warning: files list file for pac ...
- [日常] 解决PHP Warning: Module 'mysqli' already loaded in Unknown on line 0
解决PHP Warning: Module 'mysqli' already loaded in Unknown on line 0 原因:是PHP有两种方式添加扩展模块,一种是直接编译进了PHP,另 ...
- zencart后台管理中选项名称和选项内容和属性控制页面出错解决办法 WARNING: An Error occurred, please refresh the page and try again
后台管理中选项名称和选项内容和属性控制出现以下错误的解决办法WARNING: An Error occurred, please refresh the page and try again zen ...
随机推荐
- react之state两种不同的写法
我发现 state 有两种不同的写法 在构造器 constructor 中是这样的 有 this. constructor(props){//参数必须要有 super(props); this.sta ...
- 手把手教你喂养 DeepSeek 本地模型
上篇文章<手把手教你部署 DeepSeek 本地模型>首发是在公众号,但截止目前只有500多人阅读量,而在自己博客园BLOG同步更新的文章热度很高,目前已达到50000+的阅读量,流量是公 ...
- Flink内存解释
一.JobManager内存 JobManager 是 Flink 集群的控制单元. 它由三种不同的组件组成:ResourceManager.Dispatcher 和每个正在运行作业的 JobMast ...
- FreeSql学习笔记——6.修改
前言 FreeSql 提供丰富的数据库更新功能,支持单条或批量更新,支持更新指定的字段,在特定的数据库执行还可以返回更新后的记录.与删除一样,没有条件的话不会执行,避免全表修改到全表: 指 ...
- Drasi Sources SDK
什么是Drasi数据源(Source)? Source提供了与系统的连接,Drasi 可以将这些系统视为变化源.source 在 Drasi 中执行三个重要功能: 处理源系统生成的更改日志/源,并将这 ...
- Cordova基本使用(二)
cordova的打包发布版app流程简介 除了第一遍官网给的打包发布版的方法,我们可以自己多敲几次命令来实现. 基本上使用如下的几个命令就完成这个过程,先列出整个过程: 1.cordova选定ando ...
- 在GNU Hurd中感受Mach微内核的进程通信(IPC)
什么是GNU Hurd 具体的时间线已经在官方维基页面得到详细描述[0],笔者在此就简单叙述一下.在1983年Richard Stallman开启了GNU项目,目的是创建一个自由的操作系统[1].在接 ...
- 张高兴的大模型开发实战:(二)使用 LangChain 构建本地知识库应用
目录 基础概念 什么是 LangChain 什么是 Ollama 环境搭建与配置 安装 Ollama 安装 LangChain 文档加载 加载 JSON 数据 加载文件夹中的文档 文本向量化 实现问答 ...
- 抓包分析:wireshark抓不到TLS1.3数据包中证书的解决方案
近日工作中遇到需要分析使用TLS1.3协议进行通信的数据包的情况,但使用wireshark进行分析发现不能抓到服务端证书,感到诧异遂设法解决 这篇博客给出解决方案,和简单的原理分析 解决方案: 第一步 ...
- AI与.NET技术实操系列(九):总结篇 ── 探讨.NET 开发 AI 生态:工具、库与未来趋势
1. 引言 本文作为本系列的最后一篇,旨在全面探讨 .NET 生态中与 AI 相关的工具.库.框架和资源,帮助开发者了解如何在 .NET 环境中开发 AI 应用.我们将分析 Microsoft 的 A ...