React.HTMLProps <>和React.HTMLAttributes <T>有什么区别?

joj*_*ojo 6 typescript reactjs

我正在尝试为我的组件定义一个props接口,并希望它包含所有常见属性。

但发现我可以扩展两个不同的接口

interface MyProps extend React.HTMLProps<HTMLElement> interface MyProps extend React.HTMLAttributes<HTMLElement>

有什么不同?我应该使用哪一个?似乎HTMLProps包含HTMLAttributes,这是否意味着HTMLProps应该是更好的候选者?

bas*_*rat 5

HTMLProps包含的内容比还要多HTMLAttributes。这样的东西ref等等。

我过去做过以下事情:

export interface PrimitiveProps extends React.HTMLProps<HTMLDivElement> { };
Run Code Online (Sandbox Code Playgroud)

它对我来说很好

  • 或者使用 `ComponentProps`、`ComponentPropsWithoutRef` 或 `ComponentPropsWithRef` 类型...`interface PrimativeProps extends React.ComponentProps&lt;"div"&gt;`。有关详细信息,请参阅 TS React 备忘单... https://github.com/typescript-cheatsheets/react/blob/main/docs/advanced/patterns_by_usecase.md#componentprops (5认同)