question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ListItemIcon with Typescript

See original GitHub issue

I am using the latest version of the material-ui theme with typescript but I get the below error when trying to define a ListItemIcon. Any ideas what am I doing wrong? the issue seems to be down to the icon type as the children property resolves to shouldUpdate() instead of a ReactElement…

import {
    **Dashboard**
} from 'material-ui-icons';

export type AppRoute = {
    redirect?: boolean,
    to: string,
    from?: LocationDescriptor,
    path: string,
    **icon?: React.ComponentType<SvgIconProps>,**
    sidebarName?: string,
    navbarName: string,
    component?: React.ComponentType
};

const appRoutes: Array<AppRoute> = [
    {
        path: '/dashboard',
        sidebarName: 'Dashboard',
        navbarName: 'Material Dashboard',
        to: '',
        **icon: Dashboard,**
        component: DashboardPage
    },
    {redirect: true, path: '/', to: '/dashboard', navbarName: 'Redirect'}
];

<ListItem button={true} className={classes.itemLink}>
                            <ListItemIcon className={classes.itemIcon + whiteFontClasses + listItemClasses}>
                                {prop.icon}
                            </ListItemIcon>
                            <ListItemText
                                primary={prop.sidebarName}
                                className={classes.itemText + whiteFontClasses}
                                disableTypography={true}
                            />
                        </ListItem>

index.js:2177 Warning: Failed prop type: Invalid prop children of type function supplied to ListItemIcon, expected a single ReactElement. in ListItemIcon (created by WithStyles(ListItemIcon)) in WithStyles(ListItemIcon) (created by Sidebar) in div (created by ButtonBase) in ButtonBase (created by WithStyles(ButtonBase)) in WithStyles(ButtonBase) (created by ListItem) in ListItem (created by WithStyles(ListItem)) in WithStyles(ListItem) (created by Sidebar) in a (created by Link) in Link (created by Route) in Route (created by NavLink) in NavLink (created by Sidebar) in ul (created by List) in List (created by WithStyles(List)) in WithStyles(List) (created by Sidebar) in div (created by Sidebar) in div (created by Paper) in Paper (created by WithStyles(Paper)) in WithStyles(Paper) (created by Drawer) in div (created by Drawer) in Drawer (created by WithStyles(Drawer)) in WithStyles(Drawer) (created by Sidebar) in HiddenJs (created by WithWidth(HiddenJs)) in EventListener (created by WithWidth(HiddenJs)) in WithWidth(HiddenJs) (created by WithTheme(WithWidth(HiddenJs))) in WithTheme(WithWidth(HiddenJs)) (created by Hidden) in Hidden (created by Sidebar) in div (created by Sidebar) in Sidebar (created by WithStyles(Sidebar)) in WithStyles(Sidebar) (created by App) in div (created by App) in App (created by WithStyles(App)) in WithStyles(App) (created by Route) in Route in Switch in Router

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jesserwrightcommented, Jan 10, 2019

I encountered a similar issue, and was able to use the SvgIconProps from the SvgIcon export:

import RoomIcon from '@material-ui/icons/Room'
import PeopleIcon from '@material-ui/icons/People'
import { SvgIconProps } from '@material-ui/core/SvgIcon'

type Description = {
  icon: React.ReactElement<SvgIconProps>
  title: string
  body: string
}

const descriptions: Description[] = [
  {
    icon: <RoomIcon />,
    title: 'Location',
    body: 'A place containing Lines.'
  },
  {
    icon: <PeopleIcon />,
    title: 'Line',
    body: 'A place containing Sections.'
  }
]
0reactions
kwocommented, Sep 27, 2018

I had the same issue. The problem is that you must set the icon as an element with the starting and closing tags. So icon: <DashboardIcon/> (react element) instead of just icon: Dashboard (react component).

Read more comments on GitHub >

github_iconTop Results From Across the Web

ListItemIcon with Typescript #10589 - mui/material-ui - GitHub
I am using the latest version of the material-ui theme with typescript but I get the below error when trying to define a...
Read more >
How to type props for Material UI ListItem? - Stack Overflow
This is a simple solution: import React from "react"; import { ListItem, ListItemIcon, ListItemText } from "@material-ui/core"; ...
Read more >
Building a React app with TypeScript and Material Design
Learn how to build a React application using TypeScript and Material ... import { ListItem, ListItemText, ListItemIcon, Icon, Tooltip, ...
Read more >
Using React Router NavLink with a MUI ListItemButton + ...
Using React Router NavLink with a MUI ListItemButton + TypeScript. So, recently I needed to render a React ... ListItemIcon,. ListItemText,.
Read more >
The Definitive MUI MenuButton and Menu Example (With ...
I created a width and position state value, and also a ref called menuRef. The ref is of TypeScript type <HTMLButtonElement> . Take...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found