我发现最好扩展组件 props 接口,React.HTMLAttributes
因为它为您提供标准的 HTML 属性,无需任何额外配置:
interface Button1Props extends React.HTMLAttributes<Element> {
// add any custom props, but don't have to specify `children`
}
const Button1 = ({ children, ...props }: Button1Props) => (
<Button {...props}>{children}</Button>
)
如果你想强制children
被提供,你可以通过在 props 接口中重新定义它来使其成为必需的:
interface Button1Props extends React.HTMLAttributes<Element> {
children: React.ReactNode
// add any custom props, but don't have to specify `children`
}
const Button1 = ({ children, ...props }: Button1Props) => (
<Button {...props}>{children}</Button>
)