Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed prop type: Invalid prop children of type array supplied to ForwardRef(ListItemIcon), expected a single ReactElement. #15911

Closed
ask2mahtab opened this issue May 28, 2019 · 11 comments · Fixed by #20577
Labels
component: list This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process.

Comments

@ask2mahtab
Copy link

Failed prop type: Invalid prop children of type array supplied to ForwardRef(ListItemIcon), expected a single ReactElement.
failed Proptypes

<ListItem style={{"marginTop":"-16px","marginBottom": "-7px"}} button key={i+"i"} >
<ListItemIcon key={i+ subOption.text+'d' } className={classes.menuPosition}><span key={ subOption.text+1+i+1 } className={subOption.icon}/>
<ListItemText style={{marginBottom:'-5px',marginTop:'-8px', padding:'0px 16px'}} key={i+ subOption.text+'e'} primary={ subOption.text }
className="menu-text-Color"
/>

@eps1lon
Copy link
Member

eps1lon commented May 28, 2019

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.

@nagad814
Copy link

nagad814 commented Dec 5, 2019

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

@sharique-asghar
Copy link

Removing white space around the <HomeIcon>inside <ListItemIcon> will also work.

<ListItemIcon><HomeIcon /></ListItemIcon>

@iagokrt
Copy link

iagokrt commented Apr 2, 2020

i just have the same error, wrapping all inside a fragment (</>) resolved:

<ActionButton>
   <>
      <OtherComponentButton />
      <OtherComponentButton />
    </>
</ActionButton>

@oliviertassinari oliviertassinari added component: list This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. and removed status: waiting for author Issue with insufficient information labels Apr 2, 2020
@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 2, 2020

@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';

@alielkhateeb
Copy link
Contributor

@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?

@eps1lon
Copy link
Member

eps1lon commented Apr 15, 2020

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.

@oliviertassinari
Copy link
Member

So we are referring to this case:

https://github.com/mui-org/material-ui/blob/b17e6856c33e96e1b6c5c5dfac327127cf4be705/packages/material-ui/src/Tooltip/Tooltip.d.ts#L99

What about

children?: React.ReactNode;

instead?

@alielkhateeb
Copy link
Contributor

So is it ok to change the prop children to optional in ListItemIcon?

@oliviertassinari
Copy link
Member

@ask2mahtab I think so, it would match with #20458.

@dheereshagrwal
Copy link

In my case, I was passing a boolean value to my Modal component whereas it was undefined so, it made children component invalid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: list This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants