React HTTP GET request and looping through data


The following example shows how a set of data obtained from a remote source can be rendered into a component.

We make an AJAX request using fetch, which is build into most browsers. Use a fetch polyfill in production to support older browsers. You can also use any other library for making requests (e.g. axios, SuperAgent, or even plain Javascript).

We set the data we receive as component state, so we can access it inside the render method. There, we loop through the data using map. Don't forget to always add a unique key attribute (or prop) to the looped element, which is important for React's rendering performance.

import React from 'react';

class Users extends React.Component {
  constructor() {
    this.state = { users: [] };

  componentDidMount() {
      .then(response => response.json())
      .then(json => this.setState({ users: }));

  render() {
    return (
          this.state.users.length == 0
            ? 'Loading users...'
            : => (
              <figure key={}>
                <img src={user.avatar} />

ReactDOM.render(<Users />, document.getElementById('root'));

Working example on JSBin.