I am creating routes from an array of menuItems. I have written a recursive function, which works fine but I think the code can be improved. So, I need you guys to review my code and suggest me some changes.
Here is the code:
const accumulator = [];
const renderRoutes = (_menuItems) => {
_menuItems.forEach((menuItem) => {
accumulator.push(
<Route
exact
path={menuItem.path}
render={props => (
<Scene
{...props}
menuItem={menuItem}
/>
)}
/>,
);
if (menuItem.children) {
renderRoutes(menuItem.children);
}
});
return accumulator;
};