Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to render jsx calling non component function in React?

I adding return before jsx but it stops iteration

function App(){

  function recursion(object){
    for(let key in object){
      if(typeof object[key] === 'object'){
        <div>{key}</div>  // this jsx needs to render
        recursion(object[key])
      } else {
        <div>{key}</div>  // and this one
      }
    }
  }

  return {
    <>
      {recursion(someObject)}
    </>
  }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to return the JSX; it can’t just be lying around in the function. Something like this should do the trick:

function App(){

  function recursion(object){
    const ret = [];

    for(let key in object){
      if(typeof object[key] === 'object'){
        ret.push (
          <>
            <div>{key}</div>
            {recursion(object[key])}
          </>
        );
      } else {
        ret.push(<div>{key}</div>);
      }
    }

    return ret;
  }

  return (
    <>
      {recursion(someObject)}
    </>
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading