Author avatar

Raphael Alampay

Create a Stateless Component for an Array of JSON Objects in React

Raphael Alampay

  • Sep 25, 2020
  • 5 Min read
  • 113 Views
  • Sep 25, 2020
  • 5 Min read
  • 113 Views
Web Development
Front End Web Development
Client-side Frameworks
React

Introduction

Binding an array to a set of DOM elements (JSX) in React.js is a common task in app development. Often, you need to list a set of objects and their corresponding attributes, such as those found in product catalogs, member records, or lists of transactions. In this guide, we will take a look at some considerations when binding a list of objects in a React.js component's interface.

Setup

Suppose that you are creating a React.js component that lists a set of products a customer can buy in a grocery store. Each product has an id, name, and price. Data might come from anywhere, such as an API or another component. In any case, you can expect that an array of items will look like the following:

1
2
3
4
5
[
  { id: 1, name: "Apple", price: 25.00 },
  { id: 2, name: "Oranges", price: 20.00 },
  { id: 3, name: "Grapes", price: 22.00 }
]
JSON

This type of data will be passed as a props attribute called items within your component.

First, create a component that looks like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import React from 'react';

export default class ItemsList extends React.Component {
  constructor(props) {
    super(props);

    this.state = {}
  }

  render() {
    return (
      <table>
        <thead>
          <th>
            Item
          </th>
          <th>
            Price
          </th>
        </thead>
        <tbody>
        </tbody>
      </table>
    );
  }
}
javascript

Notice that you don't have any state to maintain and that this component returns a single element in the form of a <table>. Everything will be read from props instead of state. Within the <table>, React.js requires that you include a <tbody> before inserting rows in it. Otherwise, it will complain that <tr> is not a valid child element of <table>.

Populating the Table

Instead of rendering the array of items directly to render(), create a separate function to handle the building of DOM elements:

1
2
3
4
5
6
7
8
renderRows() {
  return  this.props.items.map(function(o) {
            return  <tr key={"item-" + o.id}>
                      <td>{o.name}</td>
                      <td>{o.price}</td>
                    </tr>
          });
}
javascript

Notice first that the code references this.props and assumes that it will have an attribute items. Since it's calling from this.props, it expects that this data will be passed from an external component. Second, the code uses the map function of a JavaScript array to iterate through this.props.items, which accepts a function that returns a <tr> element. Finally, the <tr> element will contain a key attribute. This is a requirement for React.js wherein each child element of a parent element that is dynamically generated has to be assigned a key unique within the parent element. Since it is assumed that each item will contain an id unique to it, the code uses that information to generate the key value of item-someItemId.

Invoke the function within the component's render() inside the <tbody> tag:

1
2
3
<tbody>
  {this.renderRows()}
</tbody>
jsx

These items will change if a component calling ItemsList passes a new set of values for items via props.

Overall Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React from 'react';

export default class ItemsList extends React.Component {
  constructor(props) {
    super(props);

    this.state = {}
  }

  renderRows() {
    return  this.props.items.map(function(o) {
              return  <tr key={"item-" + o.id}>
                        <td>{o.name}</td>
                        <td>{o.price}</td>
                      </tr>
            });
  }

  render() {
    return (
      <table>
        <thead>
          <th>
            Item
          </th>
          <th>
            Price
          </th>
        </thead>
        <tbody>
          {this.renderRows()}
        </tbody>
      </table>
    );
  }
}
javascript

Loading Values

Suppose that you want to test it out. From an external JavaScript section in your app, invoke ItemsList with some data in a DOM element react-root-div:

1
2
3
4
5
6
7
8
9
10
var items = [
  { id: 1, name: "Apple", price: 25.00 },
  { id: 2, name: "Oranges", price: 20.00 },
  { id: 3, name: "Grapes", price: 22.00 }
];

ReactDOM.render(
  <ItemsList items={items} />,
  document.getElementById("react-root-div")
);
javascript

You should see the table render with the name of the item in the first column and its price in the second column. To add more rows to the table, add more objects to items, making sure that each object has an id different from the existing objects in the array.

Conclusion

In this guide, you've created a component that has no state but populates a list of JavaScript objects often derived from a JSON object. The component renders these items dynamically, and each child element rendered should have a unique key within its parent element. Because it is a stateless component, you can easily plug this component in other parts of your React.js app that need a list rendered of items.

For an extra challenge, see if you can integrate this in your own React.js app where data is coming from an external API instead of being hardcoded.

1