【发布时间】:2020-07-20 01:32:37
【问题描述】:
我正在尝试编写一个包装器组件,该组件将决定是让ListItem 呈现react-router-dom Link 还是button
// inside some component
let props
if ("uri" in navItem) {
props = { component: Link, to: navItem.uri }
} else {
props = { button: true, onClick: navItem.onClick }
}
return (
<ListItem {...props}>
...some more code ...
</ListItem
)
它在ListItem 上抱怨说没有超载匹配这个调用。我猜错误是因为没有输入props 变量。但是,我无法理解如何输入这个,这对于我的 TS 水平来说太复杂了。
我可以得到一些关于如何输入这些的指示吗?
编辑:
要添加更多信息,我只是想把它干掉:
export type NavItem = Omit<BaseNavItem, 'onClick'> | Omit<BaseNavItem, 'uri'>;
type BaseNavItem = {
icon: React.ReactNode;
label: string;
uri: string;
onClick: () => void;
};
// MyComponent.tsx
// it will render Link or buttons depending
// on whether uri or onClick was passed in the navItem
if ("uri" in navItem) {
return (
<li>
<ListItem key={label} component={RouterLink} to={navItem.uri}>
<ListItemIcon>{icon}</ListItemIcon>
<ListItemText primary={label} />
</ListItem>
</li>
)
} else {
return (
<ListItem key={label} button onClick={navItem.onClick}>
<ListItemIcon>{icon}</ListItemIcon>
<ListItemText primary={label} />
</ListItem>
)
}
【问题讨论】:
-
你能给第一个例子更多的代码吗..
-
添加了一些额外的代码,但不确定还有什么相关的
标签: reactjs typescript material-ui