我已經將 Next.js 鏈接作為帶有打字稿的自定義 React 函式,因為“鏈接”需要“href”屬性才能在我想要的任何地方呼叫它。所以我不能把它用作一個按鈕,它在表單中用作提交按鈕
import NextLink, { LinkProps } from "next/link";
import { HTMLProps, FC } from "react";
export const LinkButton: FC<LinkProps & HTMLProps<HTMLAnchorElement>> = ({
as,
children,
href,
replace,
scroll,
shallow,
passHref,
className,
title,
onClick,
...rest
}) => (
<NextLink
as={as}
href={href}
passHref={passHref}
replace={replace}
scroll={scroll}
shallow={shallow}
>
<a className={className} {...rest}>
{children || title}
</a>
</NextLink>
);
當我在某個地方使用它時,它給了我這個錯誤
<LinkButton onClick={() => alert("hello")} title="Find Events" />
Property 'href' is missing in type '{ onClick: () => void; title: string; }' but required in
type 'LinkProps'.
我怎么能把 href 設為可選,那么如果它沒有被呼叫,我可以有條件地添加一個“”標簽,除了“”標簽
uj5u.com熱心網友回復:
您可以省略href屬性并定義為可選:
Omit<LinkProps & HTMLProps<HTMLAnchorElement>, "href"> & { href?: string }
然后在組件道具解構中,您需要指定默認值
...
href = "",
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/355950.html
標籤:javascript 反应 打字稿 下一个.js
