I have created a function and, depending on the case, I wish to recall the same function
buildRoute(listRoutes){
return(
<Switch>
{listRoutes.map((prop, key) => {
if(prop.subMenus != null){
if(prop.path !== undefined){
return
this.routes(prop, key);
this.buildRoute(prop.subMenus);
}else{
return this.buildRoute(prop.subMenus);
}
}else{
return this.routes(prop, key);
}
})}
</Switch>
)
}
routes(prop, key){
if (prop.redirect){
return <Redirect from={prop.path} to={prop.to} key={key} />;
}else if(prop.private){
return <PrivateRoute authenticated={JSON.parse(localStorage.getItem(IS_AUTHENTICATED))} path={prop.path} component={prop.component} key={key} />;
}else{
return <Route path={prop.path} component={prop.component} key={key} />;
}
}
But I have this error : VM3056 53.chunk.js:89036 Warning: Each child in an array or iterator should have a unique "key" prop.
Check the render method of APP.
in Switch (at Dashboard.jsx:84)
in APP (created by WithStyles(APP))
in WithStyles(APP) (created by Route)
in Route (at src/index.js:46)
in Switch (at src/index.js:42)
in Router (at src/index.js:41)
in I18nextProvider (at src/index.js:40)
My listRoute :
const dashboardRoutes = [
{
private: true,
path: "/private/dashboard",
sidebarName: "menu.sidebarName.dashboard",
navbarName: "header.navbarName.dashboard",
icon: Dashboard,
component: DashboardPage
},
{
private: true,
path: "/private/MENU1",
navbarName: "header.navbarName.MENU1",
component: MENU1,
sidebarName: "menu.sidebarName.MENU1",
code: "MENU1",
icon: "content_paste",
subMenus:[
{
subMenu: true,
private: true,
path: "/private/MENU1_SOUS_MENU1",
sidebarName: "menu.sidebarName.MENU1_SOUS_MENU1",
navbarName: "header.navbarName.MENU1_SOUS_MENU1",
code: "MENU1_SOUS_MENU1",
icon: "content_paste",
component: MENU1_SOUS_MENU1
},
{
subMenu: true,
private: true,
path: "/private/MENU1_SOUS_MENU2",
sidebarName: "menu.sidebarName.MENU1_SOUS_MENU2",
navbarName: "header.navbarName.MENU1_SOUS_MENU2",
code: "MENU1_SOUS_MENU2",
icon: "content_paste",
component: MENU1_SOUS_MENU2
}
]
},
{
sidebarName: "menu.sidebarName.MENU12",
code: "MENU12,
icon: "content_paste",
subMenus:[
{
subMenu: true,
private: true,
path: "/private/MENU2_SOUS_MENU1",
sidebarName: "menu.sidebarName.MENU2_SOUS_MENU1",
navbarName: "header.navbarName.MENU2_SOUS_MENU1",
code: "MENU2_SOUS_MENU1",
icon: "content_paste",
component: MENU2_SOUS_MENU1
},
{
subMenu: true,
private: true,
path: "/private/MENU2_SOUS_MENU2",
sidebarName: "menu.sidebarName.MENU2_SOUS_MENU2",
navbarName: "header.navbarName.MENU2_SOUS_MENU2",
code: "MENU2_SOUS_MENU2",
icon: "content_paste",
component: MENU2_SOUS_MENU2
}
]
},
.....
{
redirect: true,
path: "/private",
to: "/private/dashboard"
}
]
keyprop reactjs.org/docs/lists-and-keys.html#keyslistRoutescontents passed tobuildRoute?return this.routes(prop, key); this.buildRoute(prop.subMenus);this will only return thethis.routes(prop, key);