【发布时间】:2021-11-18 01:36:06
【问题描述】:
我无法为 react 子级设置正确的类型。
export const recursiveCloneChildren = (
children: React.ReactNode,
handleChildChange,
disableContent: boolean,
) => {
return React.Children.map(children, (child) => {
if (!isObject(child)) {
return child;
}
let childProps = {
...child.props,
disabled: child.props.disabled || disableContent,
};
const requiredOrValidatableChildProps = {
...childProps,
checkValidationState: handleChildChange,
};
if (child.props.required || child.props.validatable) {
childProps = {
...requiredOrValidatableChildProps,
};
}
if (child.props.children) {
childProps.children = recursiveCloneChildren(child.props.children, handleChildChange, disableContent);
}
return React.cloneElement(child, childProps);
});
};
我收到了这个错误
类型“{}”上不存在属性“props”| ReactElement |反应节点数组 | ReactPortal'。
类型“{}”上不存在属性“props”。
我尝试直接为孩子设置类型( React.ReactChild ),并在孩子身上显示另一个错误。 怎么解决?
【问题讨论】:
-
什么是`React.Children.map`?你是说
children.map(child => {return something..]) -
通常你只需要让孩子通过被渲染为
{children},它被输入为React.ReactNode。这个组件非常不习惯。这个组件有什么作用?它是如何使用的? -
@SanishJoseph, reactjs.org/docs/react-api.html#reactchildrenmap
-
@AlexWayne 它不是一个组件,它是一个辅助函数。
-
我在 Form 组件中递归克隆子组件,该组件将处理表单验证和子验证。
标签: javascript reactjs typescript