【发布时间】:2022-02-01 20:27:37
【问题描述】:
Container 是从 @material-ui/core 扩展而来的组件,它不接受 React.ReactNode 类型的子组件。
Layout.tsx:
import { Container } from "@material-ui/core";
type LayoutProps = {
children: React.ReactNode;
};
function Layout({ children }: LayoutProps) {
return (
<div>
<Container>{children}</Container>
</div>
);
}
它在 Container 被替换为 div 标签时起作用。所以,问题在于 MaterialUI 的 Container。
我发现的唯一解决方法是编辑内部容器代码,
- children: NonNullable<React.ReactNode>;
+ children?: React.ReactNode;
但更改内部 Container 代码以后会遇到一些问题。
还有其他方法可以解决这个问题吗?
我现在得到的类型错误是,
No overload matches this call.
Overload 1 of 2, '(props: { component: ElementType<any>; } & { children: boolean | ReactChild | ReactFragment | ReactPortal; disableGutters?: boolean | undefined; fixed?: boolean | undefined; maxWidth?: false | ... 5 more ... | undefined; } & CommonProps<...> & Pick<...>): Element', gave the following error.
Type 'ReactNode' is not assignable to type 'boolean | ReactChild | ReactFragment | ReactPortal'.
Type 'undefined' is not assignable to type 'boolean | ReactChild | ReactFragment | ReactPortal'.
Overload 2 of 2, '(props: DefaultComponentProps<ContainerTypeMap<{}, "div">>): Element', gave the following error.
Type 'ReactNode' is not assignable to type 'boolean | ReactChild | ReactFragment | ReactPortal'.ts(2769)
Container.d.ts(6, 5): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & { component: ElementType<any>; } & { children: boolean | ReactChild | ReactFragment | ReactPortal; disableGutters?: boolean | undefined; fixed?: boolean | undefined; maxWidth?: false | ... 5 more ... | undefined; } & CommonProps<...> & Pick<...>'
Container.d.ts(6, 5): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & { children: boolean | ReactChild | ReactFragment | ReactPortal; disableGutters?: boolean | undefined; fixed?: boolean | undefined; maxWidth?: false | ... 5 more ... | undefined; } & CommonProps<...> & Pick<...>'
【问题讨论】:
标签: reactjs typescript material-ui next.js