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

React nested iterators not returning value

I am trying to run two map functions nested within each other, and return an element inside each function. I am having trouble understanding where to put the return(s). I have tried a combination of numerous things. When I am just returning elements from the inner iterator, i have no problem. It’s when I add an element to the external iterator that it breaks down

{  chatList.data[0][0].map((message, messageIndex) => (

   return<IonItem key={messageIndex}>{message}</IonItem>
     return chatList.data[0][1].map((participant, participantIndex) => {
        return <IonItem key={participantIndex}>
          
            <IonLabel>
                <h2>{participant.identity}</h2>
            </IonLabel>
        </IonItem>
        })
     ))
 }

>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

Since you can only return once from the map function (everything after that will be unreachable), you need to return a single element. This means wrapping multiple elements in a container, such as a <div/>. So it will look something like this:

{chatList.data[0][0].map((message, messageIndex) => (   
  <div>
    <IonItem key={messageIndex}>{message}</IonItem>
    {chatList.data[0][1].map((participant, participantIndex) => (
      <IonItem key={participantIndex}>          
        <IonLabel>
          <h2>{participant.identity}</h2>
        </IonLabel>
      </IonItem>)
    )}
  </div>)
)}
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