reactjs - 在与 Typescript 的反应中正确注释 HOC
问题描述
我在正确注释以下 HOC 时遇到了困难。我不知道要为函数的返回类型添加什么,也不知道如何注释道具。我总是遇到错误。
const withAuthentication = <Props extends object>(
Component: React.ComponentType<Props>
) => {
const WithAuthentication = (props: Any) => {
const { firebase } = props
return (
<AuthUserContext.Provider value={authenticated}>
<Component {...(props as Props)} />
</AuthUserContext.Provider>
)
}
return withFirebase(WithAuthentication)
}
有谁知道如何解决这个问题?非常感谢您的帮助!
解决方案
在 typescript 中指定匿名函数表达式的返回类型的正确方法如下所示。类型说明在参数声明之后
const withAuthentication = (
Component: React.ComponentType<Props>
) : React.Component<Props> => {
const WithAuthentication = (props: any) => {
const { firebase } = props;
return (
<AuthUserContext.Provider value={authenticated}>
<Component {...(props as Props)} />
</AuthUserContext.Provider>
);
};
return withFirebase(WithAuthentication);
};
推荐阅读
- cuda - 如何在头文件上使用带有 CUDA 的 clangd 文本突出显示
- javascript - 编辑并单击 Enter 后在下一个 html 输入中设置焦点
- java - 未解决的要求:导入包:com.docusign.esign.api
- c++ - OpenGL 片段着色器不兼容
- google-cloud-storage - Google Cloud 存储上的访问权限已更改
- c++ - Why does storing an 32-bit unsigned int value of -1 in SQLite DB and reading it back result in the value getting changed to half?
- drupal-8 - 如何在 Drupal 8 中导出/导入内容?
- erlang - “FunT”的 Beam 文件格式
- mongodb - Mongodb 无法为分片 mongo db 集群创建 CN 作为 fqdn 的证书
- amazon-web-services - 缓冲 s3 对象输入