Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using for loops and switch cases in React to dynamically render different components

I am trying to render components conditionally using switch case in React JSX. I am trying to build something that reads from a specific json structure and renders the data. Since there can be many different components and data, I am trying to render it dynamically. See my code below, I am not getting any errors but the components aren't getting rendered. Inside my html , I can only see . This means the loop isn't working. I tried using the same loop in vanilla JS and it works.

var myPackage = [{     sectionInfo:[{         elementType: 1,         text: "This is text from element type 1"     }] }, {     sectionInfo:[{         elementType: 2,         text: "This is text from element type 2"     }] }]; var App = React.createClass({      render: function(){         var elements = [];         elements = myPackage.map(function(myObj){             myObj.sectionInfo.map(function(myObj1){                 switch(myObj1.elementType){                     case 1: return(                                 <Component1 text = {myObj1.text}/>                             );                             break;                     case 2: return(                                 <Component2 text = {myObj1.text}/>                             )                             break;                       }             })         });         return(             <div className="App">                 {elements}             </div>         )     } }); var Component1 = React.createClass({     render:function(){         return(             <div className = "element1">                 {this.props.text}             </div>         )     } }); var Component2 = React.createClass({     render:function(){         return(             <div className = "element2">                 {this.props.text}             </div>         )     } }); ReactDOM.render(<App/>,document.getElementById('container')); 

Edit: Made a few additions to the code, and now I am facing a new problem. Here is the new code:

var App = React.createClass({          render: function(){             var elements = [];             elements = myPackage.map(function(myObj){                 return(                        <div>                            myObj.sectionInfo.map(function(myObj1){                            switch(myObj1.elementType){                            case 1: return(                                     <Component1 text = {myObj1.text}/>                                 );                                 break;                            case 2: return(                                     <Component2 text = {myObj1.text}/>                                 )                                 break;                               }                         }                   </div>                 )                 });         return(             <div className="App">                 {elements}             </div>         )     } }); 

I want to render each time inside a div. So that if one section has more than 3 elements, then all 3 must be inside a div.

like image 734
Diablo3093 Avatar asked Jun 13 '16 06:06

Diablo3093


People also ask

How many ways you can conditionally render in React?

Editor's note: This tutorial was last updated on 16 June 2022 to reflect changes made in React v18. JSX is a powerful extension to JavaScript that allows us to define UI components.

Does React render on every state change?

React schedules a render every time state changes (scheduling a render doesn't mean this happens immediately, this might take time and be done at the best moment).


1 Answers

To do a switch case in JSX do it as follow:

   {{     sectionA: (       <SectionAComponent />     ),     sectionB: (       <SectionBComponent />     ),     sectionC: (       <SectionCComponent />     ),     default: (       <SectionDefaultComponent />     )   }[section]} 
like image 97
Oliv Avatar answered Sep 22 '22 12:09

Oliv