我正在尝试为我的组件定义一个props接口,并希望它包括所有公共属性。
但发现有两个不同的接口我可以扩展interface MyProps extend React.HTMLProps<HTMLElement>
和interface MyProps extend React.HTMLAttributes<HTMLElement>
有什么区别?我应该用哪一个?看起来HTMLProps包括HTMLAttributes,这是否意味着HTMLProps应该是一个更好的候选者?
我正在尝试为我的组件定义一个props接口,并希望它包括所有公共属性。
但发现有两个不同的接口我可以扩展interface MyProps extend React.HTMLProps<HTMLElement>
和interface MyProps extend React.HTMLAttributes<HTMLElement>
有什么区别?我应该用哪一个?看起来HTMLProps包括HTMLAttributes,这是否意味着HTMLProps应该是一个更好的候选者?
2条答案
按热度按时间hujrc8aj1#
HTMLProps
包含的内容比HTMLAttributes
多,比如ref
等等。我过去做过以下几件事:
对我来说一切都很顺利🌹
oxf4rvwz2#
现在您应该使用
ComponentPropsWithoutRef
或ComponentPropsWithRef
:React.HTMLProps
或React.HTMLAttributes
实际上为某些属性推断了错误的类型,或者未包含所有可能的值。HTMLProps
示例错误:HTMLAttributes
示例错误:要了解更多信息,您可以阅读备忘单,部分“为什么不是组件属性或内在元素或[元素]HTMLAttributes或HTMLProps或HTMLAttributes?”