解决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 ...
随机推荐
- 深度学习:测试是否含有gpu的环境
在我们跑深度学习的时候,绕不开的一定是gpu显卡,但是如何检测是否使用gpu那?下面我讲提供一段代码,可以检测自己是否开启了gpu或含有gpu. import torch # 检查是否支持 CUDA( ...
- PD还是QC?快充协议全解析
什么是快充协议 快充协议是一种通过提高充电效率来缩短设备充电时间的电池充电技术.它是通过在充电器和设备之间建立一种沟通机制,充电器能够根据设备的需求和状态,调整输出的电压和电流.这种沟通机制由快充协议 ...
- Deepseek学习随笔(10)--- 本地AI神器Cherry Studio & Chatbox 保姆级教程(附网盘链接)
本篇介绍的 Cherry Studio 和 Chatbox 两款工具,只需简单几步,即可实现本地化部署AI能力,支持对话.编程.绘图等多场景应用.本文将手把手教你从零开始配置使用! 一.软件下载:两步 ...
- Hive - 数据流转与DDL设计
数据流转设计 表的分类 按所有权分类可分为:外部表(外表)和托管表(内部表.内表). 按表的存储格式分类可分为:Text表.Orc表.Torc表.Holodesk表.Hyperbase表.ES表 按表 ...
- TypeError: Cannot read properties of null (reading 'level')
一.分析问题 1.一个下拉框组件的更新由另一个下拉框组件控制被动更新列表,子级下拉框的值是由父级下拉框的值调用接口获取,每次父级下拉框值的改变都会改变子级下拉框的数据源也就是会改变子级下拉框的opti ...
- Vulnhub-Troll-1靶机-ftp匿名登录+流量包分析+hydra爆破+ssh登录脚本提权
一.靶机搭建 选择扫描虚拟机 选择路径即可 二.信息收集 扫ip 靶机ip:192.168.108.144 扫开放端口 开放了ftp服务 扫版本服务信息 信息如下 21/tcp open ftp vs ...
- sql---一条sql查询语句是如何查询的、更新的
mysql分为server层和层存储引擎层 类型:InnoDB.MyISAM 查询过程 连接器:与客户端建立连接.获得权限.维持和管理命令 查询缓存:弊大于利,在8.0之后已去除 分析器:对sql语法 ...
- uniapp 截屏扫码
最近开发功能遇到个需求,用户点击某个操作之后,需要截取当前屏幕内容,并扫码识别屏幕截图中的二维码,代码如下: 首先将代码抽离到外部文件中,以便复用: // 截图 export function tak ...
- mdn拾遗-- 纯html+css实现的input的验证
关于input的验证,其实从很古老的前端时代开始就一直采用一种比较可靠的方式,就是js操作dom,今天浏览mdn时发现了h5的验证方法,很是兴奋.感觉值得一记. 说在前面的话,着重就是配合h5 + c ...
- 【技术分析】EIP-7702 场景下 EOA 授权签名的安全探讨
EIP-7702 在 2025 年即将到来的以太坊 Pectra 升级中,将会引入 EIP-7702 这个提案.其主要的内容就是使得 EOA 账户拥有了自己的 Storage ,并且可以通过 dele ...