Reactjs 如何在类组件使用的功能性高阶组件中使用props.children和TypeScript?
我开始编写一个HOC,但出现错误: 类型“ReactElement”上不存在属性“children” 使用此代码:Reactjs 如何在类组件使用的功能性高阶组件中使用props.children和TypeScript?,reactjs,typescript,Reactjs,Typescript,我开始编写一个HOC,但出现错误: 类型“ReactElement”上不存在属性“children” 使用此代码: const ToolsWrapper = (props: ReactElement): ReactElement => props.children; 然后使用此代码: const ToolsWrapper = (props: ReactElement): ReactElement => props.children; 函数工具rapper(子项:React.Com
const ToolsWrapper = (props: ReactElement): ReactElement => props.children;
然后使用此代码:
const ToolsWrapper = (props: ReactElement): ReactElement => props.children;
函数工具rapper(子项:React.ComponentType){
return():ReactElement=>(
{儿童}
);
}
导出默认工具rapper;
我得到一个错误:
类型“{children:(false | Element | undefined)[];}”不可分配给类型“(IntrinsicattAttributes&ComponentClass)|(IntrinsicattAttributes&FunctionComponent)”。
类型“{children:(false | Element | undefined)[];}”不可分配给类型“FunctionComponent”
其中toolswraper
在父级中使用
我怎样才能访问道具。儿童?您没有展示如何使用
工具说唱者,但根据您给定的类型(ReactElement
)和您试图访问儿童的事实,我猜您正在传递ReactElement
,例如:
ToolsWrapper(<div>...</div>)
(请注意,子项
属于反应节点
类型,有关详细信息,请参阅@types/react
中的index.d.ts
。)
或者使用分解:
const ToolsWrapper = ({props: {children}}: ReactElement): ReactNode => children;
props:ReactElement
没有多大意义。道具不是元素,它们是元素的属性。您对工具rapper
的预期用途是什么?您将传递什么?谢谢-首先,我只希望HOC返回它的子对象,但一旦我完成了这项工作,我还将传递一个布尔值。我也尝试过这个方法,但没有成功:consttoolswraper=({children}):ReactNode=>children代码>-绑定元素“children”隐式具有“any”类型。
const ToolsWrapper = ({props: {children}}: ReactElement): ReactNode => children;