Container component does not accept React.ReactNode typed children
See original GitHub issueThe Container component does not allow null
or undefined
as children since v4.10.2.
My component:
import React from "react";
import Container from "@material-ui/core/Container";
import Header from "./Header";
const Page: React.FC = ({ children }) => {
return (
<>
<Header />
<Container>{children}</Container>
</>
);
};
export default Page;
- The issue is present in the latest release.
- I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior 😯
Does not allow null
or undefined
children with this error message:
src/components/Page.tsx:10:45 - error TS2769: No overload matches this call.
Overload 1 of 2, '(props: { component: ElementType<any>; } & { children: string | number | boolean | {} | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ReactNodeArray | 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 'string | number | boolean | {} | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ReactNodeArray | ReactPortal'.
Type 'undefined' is not assignable to type 'string | number | boolean | {} | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ReactNodeArray | ReactPortal'.
Overload 2 of 2, '(props: DefaultComponentProps<ContainerTypeMap<{}, "div">>): Element', gave the following error.
Type 'ReactNode' is not assignable to type 'string | number | boolean | {} | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ReactNodeArray | ReactPortal'.
Type 'undefined' is not assignable to type 'string | number | boolean | {} | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ReactNodeArray | ReactPortal'.
10 <Container>{children}</Container>
~~~~~~~~~~
node_modules/@material-ui/core/Container/Container.d.ts:6:5
6 children: NonNullable<React.ReactNode>;
~~~~~~~~
The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & { component: ElementType<any>; } & { children: string | number | boolean | {} | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... 1 more ... | (new (props: any) => Component<...>)> | null) | (new (props: any) => Component<...>)> | ReactNodeArray | ReactPortal; disableGu...'
node_modules/@material-ui/core/Container/Container.d.ts:6:5
6 children: NonNullable<React.ReactNode>;
~~~~~~~~
The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & { children: string | number | boolean | {} | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ReactNodeArray | ReactPortal; disableGutters?: boolean | undefined; fixed?:...'
Found 1 error.
Expected Behavior 🤔
Maybe the API and/or use case has changed, but I think it should accept null
or undefined
as children.
Context 🔦
Your Environment 🌎
Tech | Version |
---|---|
Material-UI | v4.11.0 |
React | 16.13.1 |
TypeScript | 3.9.6 |
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (13 by maintainers)
Top Results From Across the Web
Container component does not accept React.ReactNode ...
The Container component does not allow null or undefined as children since v4.10.2. My component: import React from "react"; import Container ...
Read more >Container doesn't allow type children typescript - Stack Overflow
It does accept React.ReactNode , but the type also contains null and undefined. It is a union type of the following:
Read more >React.js: Property 'children' does not exist on type 'X'
The React.js error "Property 'children' does not exist on type" occurs when we try access the `children` property in a component for which...
Read more >Fragments - React
There is also a new short syntax for declaring them. Motivation. A common pattern is for a component to return a list of...
Read more >How Children Types Work In React 18 And TypeScript 4
When to use ReactNode, ReactElement, or JSX.Element. A common scenario when writing custom components is to allow that component to accept ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey team,
getting the same
example Sandbox https://codesandbox.io/s/relaxed-dan-oujgp?file=/src/App.tsx
Apparently, this also lead to changes in docs:
https://v4-9-14.material-ui.com/api/container/ contains
children
, but https://v4-10-2.material-ui.com/api/container/ (and 4.11) doesn’t list it anymore.