【发布时间】:2019-06-23 12:17:37
【问题描述】:
我正在构建一个自定义组件,我想将元素名称作为道具传递给组件,例如div 或p;一个HTMLElement
问题在于它必须是块或内联元素
export const MyComponent = props => {
const currentNode = useRef()
const { tag: Comp } = props
return <Comp ref={currentNode}>Strawberry Kisses!</Comp>
}
如您所见,我正在解构 prop 以获取 tag 值 (Component) 并将其作为元素传递。
目前,如下界面报错
Type '{ ref: MutableRefObject<undefined>; }' is not assignable to type 'IntrinsicAttributes'.
Property 'ref' does not exist on type 'IntrinsicAttributes'.ts(2322)
interface MyProps extends React.ClassAttributes<MyComponent> {
tag: string;
}
我基本上需要检查if (tag == HTMLElement && tag === JSX.IntrinsicElements) { return 'this is the correct type and allow it' }
任何方向的帮助表示赞赏。
【问题讨论】:
标签: reactjs typescript interface react-props