0

I am creating my application menu and have the following error in my function :

Each child in an array or iterator should have a unique "key" prop

function buildListItemWithNavLink(prop, key, activePro, listItemClasses, whiteFontClasses){
return (
  <div>
    <NavLink
      properties
    >
      <ListItem button className={classes.itemLink + listItemClasses}>
        <ListItemText
            properties
        />
      </ListItem>
    </NavLink>
    {prop.subMenus != null ? (
      prop.subMenus.map((propSubMenu, keySubMenu) => {
        if(propSubMenu.path != undefined)
          return buildListItemWithNavLink(propSubMenu, keySubMenu, activePro, listItemClasses, whiteFontClasses);
        else
          return buildListItemWithoutNavLink(propSubMenu, whiteFontClasses);
      })
    ) : null}
  </div>
);
}  

My operation is called in a var :

var links = (
<List className={classes.list}>
  {routes.map((prop, key) => {
    if (prop.redirect) return null;
    var activePro = " ";
    var listItemClasses;
    listItemClasses = classNames({
      [" " + classes[color]]: activeRoute(prop.path)
    });

    const whiteFontClasses = classNames({
      [" " + classes.whiteFont]: activeRoute(prop.path)
    });

      return buildListItemWithNavLink(prop, key, activePro, listItemClasses, whiteFontClasses);
  })}
</List>
);

I don't understand the problem. My goal is to create a menu with sub-menu

Nadun
  • 7,463
  • 1
  • 21
  • 27
dna
  • 486
  • 1
  • 9
  • 18
  • 1
    You need to add key prop to item that is returned from `buildListItemWithNavLink` function. React needs keys when rendering a list of elements inside one parent to handle efficient rendering when an item is added or removed. – h-des Dec 07 '18 at 11:52
  • I don't understand your response. – dna Dec 07 '18 at 12:51
  • please try the answer in here https://stackoverflow.com/questions/28329382/understanding-unique-keys-for-array-children-in-react-js – Nadun Dec 09 '18 at 16:18
  • Possible duplicate of [Understanding unique keys for array children in React.js](https://stackoverflow.com/questions/28329382/understanding-unique-keys-for-array-children-in-react-js) –  Dec 13 '18 at 02:23

0 Answers0