reactjs - 如何在带有 RefForwardingComponent 的反应函数组件中使用泛型?
问题描述
当我在反应中使用 forwardRef 时,我的代码如下:
interface FProps<P, T> {
name: P
birth: T
}
interface Handler {}
const Comp: RefForwardingComponent<Handler, FProps<P, T>> = <P, T>(
{ name, birth }: FProps<P, T>,
ref
) => {
useImperativeHandle(ref, () => ({
name
}))
return <div>{name}</div>
}
export default forwardRef(Comp)
但我得到了:
如何解决这个问题呢?
解决方案
泛型P
取决于函数的参数,并且仅在调用函数时才知道,因此您不能使用它来定义函数本身。相反,我们必须定义参数:
const Comp = <P, T>(
{ name, birth }: FProps<P, T>,
ref: ((instance: Handler | null) => void) | MutableRefObject<Handler | null> | null,
) => {
...
推荐阅读
- python - Python CGI 不适用于 Oracle 数据库
- mongodb - 如何从 MongoDB 集合中选择随机文档而不是以前的文档?
- node.js - 在快速后端 api 中连接 firebase 以进行 otp 身份验证
- google-apps-script - 如果用户在考试中断开连接,如何自动提交谷歌表单?
- java - Java 应用程序中的 WebView 在嵌入式 Linux ARM 硬浮点中不起作用
- powershell - 已解决 - PowerShell 在使用 Microsoft.Office.Interop.Outlook.dll 从 PST 中提取电子邮件时出现无法找到“RemoveStore”错误的重载
- python - 如何在python pandas中获取与最大值相关的所有行
- reactjs - 反应下拉问题
- postcss - 本地 @import 不会导致文本替换
- c# - 如何解决 XAML C# 中的动画问题?