Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Why doesn't my react function return the proper html codes

This is a follow-up question to my SO question. The accepted answer did resolve my problem. However I wound up with all Categories listed under the main menu. I determined it was because I was calling a push in the middle of a push.

Original relevant code:

function AddMenu(category) {
    if (category.children.length !== 0) {
        categoriesStyled.push(<SubMenu label={category.name}>
        { category.children.forEach(element => AddMenu(element) )}
        </SubMenu>);
    }
    else {
        categoriesStyled.push(<MenuItem>{category.name}</MenuItem>);
    }
}

I tried to fix this by splitting my function into two:

function AddSubMenu(category) {
    if (category.children.length !== 0) {
        return (<SubMenu label={category.name}>
            {category.children.forEach((catchild) => AddSubMenu(catchild))}
            </SubMenu>);
    }
    else {
        return (<MenuItem>{category.name}</MenuItem>);
    }
}

function AddMenu(category) {            
    if (category.children.length !== 0) {                
        categoriesStyled.push(<SubMenu label={category.name}>
            {category.children.forEach((catchild) => AddSubMenu(catchild))}
        </SubMenu>);
    }
    else {
        categoriesStyled.push(<MenuItem>{category.name}</MenuItem>);
    }
}

This shows all the main (First level) Categories but nothing under each SubMenu when the category has children. Why can't I return sub-items? Any help will be appreciated.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!