trevorsg,您需要传递按钮属性:
import * as React from 'react'
type ButtonProps = React.HTMLProps<HTMLButtonElement>
const FancyButton = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => (
<button type="button" ref={ref} className="FancyButton">
{props.children}
</button>
))
// You can Now get a ref directly to the DOM button:
const ref = React.createRef<HTMLButtonElement>()
<FancyButton ref={ref}>Click me!</FancyButton>
添加:
在最新版本的TS和@ types / react中,您也可以使用React.ComponentPropsWithoutRef<'button'>
代替React.HTMLProps<HTMLButtonElement>