如何有条件地渲染按钮 /链接Next.js组件使用Typescript
我遇到了一个非常烦人的问题,是我在有条件地渲染下一个链接链接组件或按钮元素时会遇到一堆打字稿错误。 因此,如果通过HREF支柱,我想渲染Next.js内置链接组件,否则应该是按钮。 这只是
Type 'HTMLAnchorElement' is missing the following properties from type 'HTMLButtonElement': disabled, form, formAction, formEnctype, and 11 more.ts(2322)
我不知道如何让打字稿理解的一个示例的一个示例,请理解如果存在HREF Prop,请使用(ButtonAslinkProps& Aprops),如果没有,请使用ButtonasButtonProps。
我是打字稿的新手,试图将其合作仅几周,很肯定的是,这里的类型是错误的。
这是我在这里的第一篇文章,我是如此的绝望,在互联网上任何地方都找不到与此有关的任何主题。非常感谢任何帮助。该组件的代码是波纹管:
interface BaseProps {
children: ReactNode
primary?: boolean
className?: string
}
type ButtonAsButtonProps = BaseProps &
Omit<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, keyof BaseProps> & {
href: undefined
}
type ButtonAsLinkProps = BaseProps & LinkProps
type aProps = Omit<AnchorHTMLAttributes<HTMLAnchorElement>, keyof BaseProps>
type ButtonProps = (ButtonAsLinkProps & aProps) | ButtonAsButtonProps
const Button = ({
children,
className,
href,
primary,
as,
replace,
scroll,
shallow,
passHref,
prefetch,
locale,
legacyBehavior,
...rest
}: ButtonProps): JSX.Element | undefined => {
const classNames = `${commonStyles} ${primary ? primaryStyles : secondaryStyles} ${className}`
const linkProps = {
as,
replace,
scroll,
shallow,
passHref,
prefetch,
locale,
legacyBehavior,
}
if (href && linkProps) {
return (
<Link href={href} {...linkProps}>
<a {...rest}>{children}</a>
</Link>
)
}
if (!href) {
return (
<button {...rest} className={classNames}>
{children}
</button>
)
}
}
export default Button
I ran into a very annoying problem is that I get a bunch of typescript errors, while trying to conditionally render the Next.js Link component or a button element.
So if the href prop is passed, I want to render Next.js built-in Link component, else it should be Button.
This is just one example of the error
Type 'HTMLAnchorElement' is missing the following properties from type 'HTMLButtonElement': disabled, form, formAction, formEnctype, and 11 more.ts(2322)
I don't know how to make typescript understand that please use (ButtonAsLinkProps & aProps) if the href prop is present, and use ButtonAsButtonProps if not.
I am very new to typescript, trying to work with it for only few weeks, pretty sure, that the types here are wrong.
This is my first post here, I am so desperate, can't find any topics related to this anywhere on the internet. Would really appreciate any help. The code for the component is bellow:
interface BaseProps {
children: ReactNode
primary?: boolean
className?: string
}
type ButtonAsButtonProps = BaseProps &
Omit<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, keyof BaseProps> & {
href: undefined
}
type ButtonAsLinkProps = BaseProps & LinkProps
type aProps = Omit<AnchorHTMLAttributes<HTMLAnchorElement>, keyof BaseProps>
type ButtonProps = (ButtonAsLinkProps & aProps) | ButtonAsButtonProps
const Button = ({
children,
className,
href,
primary,
as,
replace,
scroll,
shallow,
passHref,
prefetch,
locale,
legacyBehavior,
...rest
}: ButtonProps): JSX.Element | undefined => {
const classNames = `${commonStyles} ${primary ? primaryStyles : secondaryStyles} ${className}`
const linkProps = {
as,
replace,
scroll,
shallow,
passHref,
prefetch,
locale,
legacyBehavior,
}
if (href && linkProps) {
return (
<Link href={href} {...linkProps}>
<a {...rest}>{children}</a>
</Link>
)
}
if (!href) {
return (
<button {...rest} className={classNames}>
{children}
</button>
)
}
}
export default Button
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我一段时间以前的解决方案,我无法在没有一些打字的情况下设法做到这一点,也许使用较新的TS可以做到这一点。
另外,您可以从组件中删除Next.js链接的所有逻辑,然后使用它:
Here is my solution from some time ago, I could not manage to make it without some typecasting, maybe it is possible with newer version of TS.
Alternatively you can remove all the logic about Next.js Link from the component and use it like that:
您可以使用CSS来实现类似的事情:
如果您不使用尾风CSS,则可以创建自己的类并添加指针事件:样式表中的无属性在必要时禁用链接。
祝你好运!
You can achieve something like this using CSS:
If you're not using Tailwind CSS, you can create your own class and add the pointer-events: none property in your stylesheet to disable the link when necessary.
Good luck!