Failed prop type: Invalid prop children
of type array
supplied to ForwardRef(ListItemIcon)
, expected a single ReactElement.
/>
It looks like you're passing a span
and ListItemText
as children to ListItemIcon
. You can only pass a single ReactElement as the warning indicates.
My code <ListItemIcon> <HomeIcon className={classes.icon}/> </ListItemIcon>
showed the same error. When i remove the white space and moved to new line i was able to remove the error
Removing white space around the <HomeIcon>
inside <ListItemIcon>
will also work.
<ListItemIcon><HomeIcon /></ListItemIcon>
i just have the same error, wrapping all inside a fragment (>) resolved:
```
<>
>
@iagokrt Looking at the source of ListItemIcon.js, this constraint is no longer required, it seems that we can loosen it. Do you want to submit a pull request? :)
diff --git a/packages/material-ui/src/ListItemIcon/ListItemIcon.d.ts b/packages/material-ui/src/ListItemIcon/ListItemIcon.d.ts
index 029ca35a3..16fb588e5 100644
--- a/packages/material-ui/src/ListItemIcon/ListItemIcon.d.ts
+++ b/packages/material-ui/src/ListItemIcon/ListItemIcon.d.ts
@@ -6,7 +6,7 @@ export interface ListItemIconProps
* The content of the component, normally `Icon`, `SvgIcon`,
* or a `@material-ui/icons` SVG icon element.
*/
- children: React.ReactElement;
+ children: React.ReactNode;
}
export type ListItemIconClassKey = 'root';
@oliviertassinari Hi, I'm creating a pull request for this, but somehow changing the definition to children: React.ReactNode
and running yarn proptypes
generates children: PropTypes.node
in ListItemIcon.js
without .isRequired
Sorry, but what am I missing here? and how was it marked required before when it was ReactElement
?
Sorry, but what am I missing here? and how was it marked required before when it was ReactElement?
Don't worry about. ReactNode includes undefined which is why it isn't marked as required anymore.
So we are referring to this case:
What about
children?: React.ReactNode;
instead?
So is it ok to change the prop children
to optional in ListItemIcon
?
@ask2mahtab I think so, it would match with #20458.
Most helpful comment
My code
<ListItemIcon> <HomeIcon className={classes.icon}/> </ListItemIcon>
showed the same error. When i remove the white space and moved to new line i was able to remove the error