reactjs - 如何使用 TypeScript 以类型安全的方式访问 React 子元素道具?
问题描述
我正在尝试访问props
我的应用程序中的 React [Native] 组件(保证是我的自定义类型元素的实例ItemTemplate
:
const children = React.Children.toArray(this.props.children);
return children.find(t => t.props.itemKey == key);
但是,在我尝试访问的第二行中,我t.props
得到:
Property 'props' does not exist on type 'ReactElement<ItemTemplate, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | ... 13 more ... | (ReactElement<...>[] & ReactPortal)'.
Property 'props' does not exist on type 'ReactElement<ItemTemplate, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>[] & string'.
该代码确实可以正常工作,但 TypeScript (3.6.3) IntelliSense 抱怨。为什么?
解决方案
在这种情况下,您需要将子项声明或强制转换为 ReactElement。这里我有一个从 nextjs 到自定义 NavLink 的 Link 组件示例:
import { ReactElement, cloneElement } from 'react';
import Link, { LinkProps } from 'next/link';
type NavigationData = {
currentPath: string;
pathsToMatch: string[] | string;
activeClassName: string;
children: ReactElement;
};
type NavLinkProps = LinkProps & NavigationData;
export default function NavLink({
currentPath,
pathsToMatch,
activeClassName,
children,
...props
}: NavLinkProps): ReactElement {
function GetActiveLink(
currentPath: string,
paths: string | string[],
activeClassName: string
): string {
if (currentPath === '/' && paths === '/') {
return activeClassName;
} else if (currentPath !== '/' && paths.indexOf(currentPath) !== -1)
{
return activeClassName;
}
return '';
}
let className: string = children.props.className || '';
className += ` ${GetActiveLink(currentPath, pathsToMatch, activeClassName)}`;
return <Link {...props}>{cloneElement(children, { className })}</Link>;
}
通过这种方式,您可以在没有任何警告的情况下访问属性道具。
快乐的代码。:-)
推荐阅读
- google-cloud-platform - InfiniteStreamRecognize 实时 STT 示例不起作用
- optimization - 在运行时用移位替换乘以 2
- amazon-web-services - 选择或汇总针对 Cloudwatch 数据代理指标记录的维度
- wso2 - 我需要在 WSO2 Integration Studio 中应用 Aggregator Mediator
- c++ - 类型转换提升多精度整数返回错误答案
- r - 在 R 中使用 Vaultr 从 Hashicorp Vault 中提取密码
- append - Dataweave 在变换中追加
- tensorflow - 没有为任何变量/自定义损失提供梯度
- php - 共享 cPanel 托管包不提供可用于 WordPress 网站的临时 URL
- matlab - 在 Matlab R2020a 中打开脚本时出错