Using a public API to display data It's a simple app that uses a public API to display data. It works as is, but I'm seeing a lot of spaghetti code that my limited experience can't fix on its own.

import React, { Component } from "react";
import Spinner from "./components/common/Spinner";
import "../src/App.css";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [],
      isLoading: false
    };
  }

  componentDidMount() {
    const urlParams = new URLSearchParams(window.location.search);
    const city = urlParams.get("city");
    fetch(`http://opentable.herokuapp.com/api/restaurants?city=${city}`)
      .then(res =&gt; res.json())
      .then(json =&gt; {
        this.setState({
          isLoading: true,
          items: json
        });
      });
  }
  render() {
    const { isLoading, items } = this.state;
    let content = &lt;h2&gt;No Data&lt;/h2&gt;;
    if (!items.restaurants || items.restaurants === []) {
      content = &lt;h2&gt;No Data&lt;/h2&gt;;
    } else if (items.restaurants.length !== 0) {
      console.log(items);
      let itemsToArray = Object.values(items.restaurants);
      content = (
        &lt;div className="App"&gt;
          &lt;div className="row"&gt;
            &lt;div className="col-md-3"&gt;
              {itemsToArray.map(item =&gt; (
                &lt;li key={item.id} style={{ listStyleType: "none" }}&gt;
                  &lt;a
                    href={item.reserve_url}
                    style={{ color: "red" }}
                    target="_blank"
                  &gt;
                    {item.name}
                  &lt;/a&gt;{" "}
                  | &lt;span style={{ fontStyle: "italic" }}&gt;{item.address}&lt;/span&gt;{" "}
                  | Price rating:{" "}
                  &lt;span style={{ color: "red" }}&gt;{item.price}&lt;/span&gt;
                &lt;/li&gt;
              ))}
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      );
    }
    return !isLoading ? (
      &lt;div&gt;
        &lt;Spinner /&gt;
      &lt;/div&gt;
    ) : (
      &lt;React.Fragment&gt;
        &lt;div className="row"&gt;
          &lt;div className="col-md-6"&gt;
            &lt;div&gt;{content}&lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/React.Fragment&gt;
    );
  }
}

export default App;
I can think of mutliple ways to improve this code.

First, error handling. In your case, not receiving data from your API call may cause your program to crash.

You will have to add an error variable in your state :

this.state = {
    items: [],
    isLoading: true,
    error: ''
};


And add a .catch at the end of your fetch function (or use the new async/await syntax surrounded by a try/catch block).

You should also put your response formatting here instead of the rendering function, to avoid reformatting your data everytime your component is rerendered :

fetch(`http://opentable.herokuapp.com/api/restaurants?city=${city}`)
    .then(res =&gt; res.json())
    .then(json =&gt; {
        this.setState({
            isLoading: false,
            items: Object.values(json.restaurants) //Data formatting
        });
    })
    .catch(error =&gt;{
        this.setState({
            isLoading: false,
            error // Sends the error object (or message, I am not sure about the exact syntax)
        });
    });


I also noticed that you set your isLoading state to true when you finished loading and to false in your initial state so I reversed it.

Now, the rendering.

JSX allows you to put conditions inside your HTML, making the syntax of your render way more readable and avoid code duplication :

render() {
    const { isLoading, items, error } = this.state;

    return (
        &lt;div className="row"&gt;
            &lt;div className="col-md-6"&gt;
                &lt;div className="App"&gt;
                    &lt;div className="row"&gt;
                        &lt;div className="col-md-3"&gt;
                            {isLoading &amp;&amp; &lt;Spinner /&gt;}
                            {error &amp;&amp; &lt;div&gt;Error : {error}&lt;/div&gt;}
                            {!isLoading &amp;&amp; !items.length &amp;&amp; &lt;h2&gt;No Data&lt;/h2&gt;}
                            &lt;ul&gt; // Did you forget this node ?
                                {items.map(({ id, reserve_url, address, price, name }) =&gt; //Won't do anything if the items array is empty
                                    &lt;li key={id} style={{ listStyleType: "none" }}&gt;
                                        &lt;a
                                            href={reserve_url}
                                            style={{ color: "red" }}
                                            target="_blank"
                                        &gt;
                                            {name}
                                        &lt;/a&gt;{" "}
                                        | &lt;span style={{ fontStyle: "italic" }}&gt;{address}&lt;/span&gt;{" "}
                                        | Price rating:{" "}
                                        &lt;span style={{ color: "red" }}&gt;{price}&lt;/span&gt;
                                    &lt;/li&gt;
                                )}
                            &lt;/ul&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    )
}


I also deconstructed your parameters in the map function.

Working example :



class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            items: [],
            isLoading: true,
            error: ''
        };
    }

    componentDidMount() {
        const urlParams = new URLSearchParams(window.location.search);
        const city = urlParams.get("city");

        fetch(`http://opentable.herokuapp.com/api/restaurants?city=London`) // Hard coded London
            .then(res =&gt; res.json())
            .then(json =&gt; {
                setTimeout(() =&gt; { //Just to see the loading element / spinner
                    this.setState({
                        isLoading: false,
                        items: Object.values(json.restaurants)
                    });
                }, 1500)
            })
            .catch(error =&gt;{
                this.setState({
                    isLoading: false,
                    error
                });
            });
    }

    render() {
        const { isLoading, items, error } = this.state;

        return (
            &lt;div className="row"&gt;
                &lt;div className="col-md-6"&gt;
                    &lt;div className="App"&gt;
                        &lt;div className="row"&gt;
                            &lt;div className="col-md-3"&gt;
                                {isLoading &amp;&amp; &lt;div&gt;Loading...&lt;/div&gt;}
                                {error &amp;&amp; &lt;div&gt;Error : {error}&lt;/div&gt;}
                                {!isLoading &amp;&amp; !items.length &amp;&amp; &lt;h2&gt;No Data&lt;/h2&gt;}
                                &lt;ul&gt;
                                    {items.map(({ id, reserve_url, address, price, name }) =&gt;
                                        &lt;li key={id} style={{ listStyleType: "none" }}&gt;
                                            &lt;a
                                                href={reserve_url}
                                                style={{ color: "red" }}
                                                target="_blank"
                                            &gt;
                                                {name}
                                            &lt;/a&gt;{" "}
                                            | &lt;span style={{ fontStyle: "italic" }}&gt;{address}&lt;/span&gt;{" "}
                                            | Price rating:{" "}
                                            &lt;span style={{ color: "red" }}&gt;{price}&lt;/span&gt;
                                        &lt;/li&gt;
                                    )}
                                &lt;/ul&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        )
    }
}

ReactDOM.render(&lt;App/&gt;, document.getElementById('root'))
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.5.2/umd/react.production.min.js"&gt;&lt;/script&gt;
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.5.2/umd/react-dom.production.min.js"&gt;&lt;/script&gt;
&lt;div id='root'/&gt;