我有一个函数组件(带有TypeScript),假设它叫做Button。我从Material-UI中将Button作为BaseButton导入,设置样式,添加一些很酷的元素(我在这里删除了不必要的代码),然后将它导出为我自己的Button。稍后,当我在代码中的某个地方使用这个导出的button时,我想将Link组件(从react-router-dom导入)作为一个‘组件’属性传递给这个按钮。我应该在ButtonProps接口中使用什么类型?另外,'to‘prop应该被指定为一个字符串吗?
component/button.tsx
import * as React from 'react';
import { Button as BaseButton } from '@material-ui/core';
interface ButtonProps {
component: ???;
to: string;
}
export const Button: React.FC<ButtonProps> = ({ component, to }) => (
<BaseButton component={component} to={to} />
)
。
some other place
import { Button } from 'components/button';
import { Link } from 'react-router-dom';
(...)
<Button component={Link} to={'/'} />
(...)
发布于 2020-03-06 22:46:09
< Link >标记不是可视组件,而是用于添加导航功能的包装器,material-ui的用途正好相反,因此应该导航如下内容
export const Button: React.FC<ButtonProps> = ({ to }) =>
<Link to={to}
<BaseButton /> // your own styled compnent
</Link>
)
https://stackoverflow.com/questions/60566252
复制相似问题