Reactjs React.FC泛型
我正在尝试构建一个组件,它要么返回路由器链接,要么根据道具提供按钮。 以下是我的代码:Reactjs React.FC泛型,reactjs,typescript,Reactjs,Typescript,我正在尝试构建一个组件,它要么返回路由器链接,要么根据道具提供按钮。 以下是我的代码: import React from 'react'; import Button from '@material-ui/core/Button'; import { Link } from 'react-router-dom'; type OwnProps = { url: string } | { onClick: () => void }; const NavButton: React.FC&l
import React from 'react';
import Button from '@material-ui/core/Button';
import { Link } from 'react-router-dom';
type OwnProps = { url: string } | { onClick: () => void };
const NavButton: React.FC<OwnProps> = ({ url, onClick, children }) => (
<Button component={url ? Link : undefined} to={url} onClick={onClick}>
{children}
</Button>
);
export default NavButton;
从“React”导入React;
从“@material ui/core/Button”导入按钮;
从'react router dom'导入{Link};
键入OwnProps={url:string}{onClick:()=>void};
const NavButton:React.FC=({url,onClick,children})=>(
{儿童}
);
导出默认导航按钮;
然而,我得到一个错误,说:
TS2339:类型“PropsWithChildren”上不存在属性“url”
TS2339:类型“PropsWithChildren”上不存在属性“onClick”
附言:我不希望两个道具都包含在界面中。我想要这个或那个。您可以像这样键入
OwnProps
:
{ url: string, onClick?: never } | { url?: never, onClick: () => void }
@rid在这一点上返回值并不重要,因为它根本不允许我使用这些道具谢谢!我完全忘记了从不打字