Iterating/Loop Through Your Component Property In Render Function In React

I understand that you need to build some UI elements dynamically in your component’s render function in React. Yes! the only way is to loop through the items, you can either use a for loop or a map function to do so. But the real question is, are we allowed to do that in react? Unfortunately, not in a direct way, you may face some difficulty especially if you come from an Angular background.

Introduction

I understand that you need to build some UI elements dynamically in your component’s render function in React. Yes! The only way is to loop through the items, you can either use a for loop or a map function to do so. But the real question is, are we allowed to do that in React? Unfortunately, not in a direct way, you may face some difficulty, especially if you come from an Angular background. You were probably getting an error as "unused expression, expected an assignment or function call, and now you are here in this page for an easy solution. It isn’t that hard to achieve, I will explain how. I hope you will like it. You can always read this article in my blog here.

Background

I have an array of addresses and I need to show this in a print template, so I thought of creating a separate component which iterates through the item property. And each array element has its own property and I wanted to generate labels for each item. Here, I am going to explain how I did it.

Let’s start coding

The first thing is, I have an interface for the address list of properties as below.

  1. import { IAddressData } from "../../../interfaces/IAddressData";  
  2. export interface IAddressListProps {  
  3.    items: IAddressData[];  
  4. }  

Now, I have created a component called PrintAddressList.tsx and written some code.

  1. import * as React from "react";  
  2. import { IAddressListProps } from '../../detailPage/components/interfaces/IAddressListProps';  
  3. export class PrintAddressList extends React.Component<IAddressListProps, {}>{  
  4.    constructor(props: IAddressListProps){  
  5.       super(props);  
  6.    }  
  7. public render(): React.ReactElement<IAddressListProps> {  
  8.    return(  
  9.       <div>  
  10.       </div>  
  11.       );  
  12.    }  
  13. }  

And I need to have my custom labels for each address inside the div element in the render function. To do so, I had created a private function called createAddressCard which will loop through my props and return the HTML I need.

  1. private createAddressCard = () => {  
  2.    let parent = [];  
  3.    this.props.items.map((address) => {  
  4.       parent.push(<div style={{ padding: '10px', border: '1px solid #ccc', margin: '5px'}}>  
  5.          <label>{config.colNames.addressList.clmnAddressType}: </label>  
  6.          <label>{address.Adressart}</label><br />  
  7.          <label>{config.colNames.addressList.clmnCompanyName}: </label>  
  8.          <label>{address.Firma}</label><br />  
  9.          <label>{config.colNames.addressList.clmnPlaceName}: </label>  
  10.          <label>{address.Ort}</label><br />  
  11.          <label>{config.colNames.addressList.clmnZipCode}: </label>  
  12.          <label>{address.PLZ}</label><br />  
  13.          <label>{config.colNames.addressList.clmnTelephone}: </label>  
  14.          <label>{address.Telefon}</label><br />  
  15.       </div>);  
  16.     });  
  17. return parent;  
  18. }  

Now I can easily call this function inside the render function.

  1. public render(): React.ReactElement<IAddressListProps> {  
  2.    return(  
  3.       <div>  
  4.          {this.createAddressCard()}  
  5.       </div>  
  6.    );  
  7. }  

Below is the complete code for the component.

https://gist.github.com/SibeeshVenu/d2e11ed225a0568ded27c6f8fec0956c

Conclusion

In this post, we have learned how we can iterate our components properties inside a render function in the React component, for creating custom elements dynamically.

Your turn. What do you think?

Thanks a lot for reading. Did I miss anything that you may think is needed? Did you find this post useful? If yes, please like/share/clap for me. Thanks in advance.