我想创建一个组件,它从 props 和 child 接受其类型(例如“div”、“span”)并渲染它们,该组件还支持引用转发和 className prop。
问题是我收到错误'className' does not exist on type 'IntrinsicAttributes,并且无法找出导致该错误的原因。
我尝试将我提供的类型更改为forwardRef不同HTMLElement的类型,在谷歌上搜索类似的组件,但这没有什么帮助。
HTMLElement例如,当我更改为 ,HTMLDivElement并更改<Component ...>为此错误时<div ...>,该错误就会消失。所以问题可能是类型错误 ( HTMLElement)
这是组件代码:
import React, { ReactChildren, ComponentType } from "react"
interface MyComponentProps {
children: ReactChildren
component: string | ComponentType
className: string
}
const MyComponent = React.forwardRef<HTMLElement, MyComponentProps>(
(props, ref) => {
const { children, className, component: Component, ...otherProps } = props
return (
<Component className={className} ref={ref} …Run Code Online (Sandbox Code Playgroud)