解决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给当前元素添加一个类以及key的作用
给当前元素添加一个类是通过className来处理的: 引入css;直接from XXXX import React, { Component } from "react"; // ...
- 开源数据库生态遇新变数,天翼云TeleDB提供企业数据管理更优解!
近日,知名开源大规模并行处理 (MPP) 数据库Greenplum的源代码在其官方GitHub页面突然消失,引发了用户和开发者的广泛关注, PostgreSQL生态系统或将产生新变数.开源软件在面对商 ...
- tomcat切割日志(log_cut.sh)
vim log_cut.sh #!/bin/bash #auther by wangxp #tomcat日志位置 LOG_DIR=/u01/yhpt/yhpt_cda/sdmp/logs #切割日志名 ...
- 耳分解、双极定向和 P9394 Solution
耳分解 设无向图 \(G'(V',E')\subset G(V,E)\),简单路径或简单环 \(P:x_1\to \dots \to x_k\) 被称为 \(G\) 关于 \(G'\) 的耳,当且仅当 ...
- Luogu P1613 跑路 题解 [ 蓝 ] [ 倍增 ] [ Floyd 最短路 ] [ 状压 dp ]
跑路:绝佳倍增好题,思路是化 \(2^k\) 为 \(1\) ,倍增起预处理作用. 最近不知道是撞了什么运,前一脚看的是绿题,写完之后交一发,发现直接被 lxl 升蓝了,血赚. 思路:Floyd 首先 ...
- 【Java基础总结】集合框架
集合和数组的区别 集合只存储对象,长度是可变的: 数组既可以存储基本数据类型,又可以存储对象,但长度是固定的. 1. Collection接口 代码演示 1 List<String> c1 ...
- 用python做时间序列预测十:时间序列实践-航司乘客数预测
本文以航司乘客数预测的例子来组织相关时间序列预测的代码,通过了解本文中的代码,当遇到其它场景的时间序列预测亦可套用. 航司乘客数序列 预测步骤 # 加载时间序列数据 _ts = load_data() ...
- [IOI2020] 连接擎天树 题解
第一道函数交互 \(+\ luogu\) 最劣解,这不得发篇博客鼓励一下. 引理 \(1\):若 \(p_{i,j}>0,p_{i,k}>0,p_{j,k}=0(i\ne j\ne k)\ ...
- [SCOI2007] 蜥蜴 题解
发现实际上就是在求有多少只蜥蜴能逃出来. 发现可以将柱子拆成入点和出点两部分,自己的出点向别人的入点连边,自己的入点向自己的出点连边.最后再加一个超级源点 \(S\),连接所有有蜥蜴的柱子入点:再加一 ...
- Typora - typora主题样式
主题名称:github_harley.css(随便命名) 效果:Mac风格的代码块.更舒适的引用块风格. css harley01 /* 代码块主题 */ /* 顶部 */ .md-fences { ...