javascript - 是否可以使用 TypeScript 动态渲染带有道具的元素?
问题描述
我一直在挖掘 SO 和整个网络以寻求解决方案,但我似乎无法确定它。
我有两个组件,Link
和Button
. 长话短说:它们是<a>
和<button>
元素的包装器,但增加了选项,例如右侧的 V 形、左侧的图标、全角模式等。
这是我到目前为止所拥有的(这是在 typescriptlang.org/play 上运行的代码):
type Variant = "primary" | "secondary" | "tertiary";
interface CommonProps {
variant?: Variant;
showChevron?: boolean;
icon?: IconDefinition;
fullWidth?: boolean;
small?: boolean;
}
interface LinkOnlyProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
href: string;
}
interface ButtonOnlyProps
extends React.ButtonHTMLAttributes<HTMLButtonElement> {
onClick: React.MouseEventHandler<HTMLButtonElement>;
}
export type LinkProps = CommonProps & LinkOnlyProps;
export type ButtonProps = CommonProps & ButtonOnlyProps;
export const Link = (props: LinkProps) => {
const {
children,
showChevron,
icon,
fullWidth,
variant,
small,
className,
...rest
} = props;
const { classes } = initButton(props);
return (
<a className={classes} {...rest}>
<Content {...props} />
</a>
);
};
export const Button = (props: ButtonProps) => {
const {
children,
showChevron,
icon,
fullWidth,
variant,
small,
className,
...rest
} = props;
const { classes } = initButton(props);
return (
<button className={classes} {...rest}>
<Content {...props} />
</button>
);
};
我尝试将Link
andButton
组件的通用逻辑提取到单个Component
中,但是当我传播...rest
道具时,我得到 TypeScript 对我大喊大叫。从错误来看,似乎是因为我无法解释<a>
道具被传播到<button>
元素上的可能性,反之亦然。
我想保留Link
和Button
作为单独的组件,而不是将类型指定为道具,以便在实现组件时开发人员的意图很清楚。
有没有可能将通用逻辑提取到一个中心组件中,Link
并且Button
可以简单地充当包装器?例如:
export const Link = (props: LinkProps) => {
return <Component element="a" {...props} />;
}
export const Button = (props: ButtonProps) => {
return <Component element="button" {...props} />;
}
解决方案
as any
在传播rest
我的道具时能够解决类型断言:
return (
<Element className={classes} {...(rest as any)}>
<Content {...props} />
</Element>
);
推荐阅读
- java - 获取特定键值的索引号
- javascript - 实施偏移后,传单折线在某些缩放级别中显示错误
- lldb - 如何在 Xcode 控制台中删除 Hook 1 (expr -- @import UIKit display @import Appkit)
- python - 如何调试从 Excel VBA 脚本调用的 Python 脚本?
- php - 在使用 Apache24 时使用 php 连接到 mysql
- spring - spring 框架/spring boot/module-info.java 文件
- css - 大纲不会进入 Bootstrap 4 的 btn-group 按钮
- javascript - 当我不小心关闭浏览器时,`onbeforeunload` 功能没有检测到;当我使用 ajax
- jquery - 如何加载仅显示最大修订的数据表
- typescript - 打字稿 - 有什么理由为什么函数关键字在类之外而不是在类中被接受?