title: React Hooks & Async Await: Handling errors with Axios call
body: <p>I'm new to <code>hooks</code> and <code>async/await</code>. I'm trying to handle errors in an Axios call and I'm unsure of how to use <code>then/catch</code> or <code>try/catch</code> to handle errors with my API call.</p>
  <p>In class based React I would handle errors like this:</p>
  <pre class="lang-js prettyprint-override"><code>componentDidMount() {
         axios.get('url')
          .then((res) =&gt; {
              // handle response here
          })
          .catch((err) =&gt; {
              //handle error here
          })
     };
  </code></pre>
  <p>How can I handle Axios error handling when using <code>useEffect</code> and <code>async/await</code>? I've seen examples using <code>try/catch</code> but I can't get it to work in my code.</p>
  <p>How can I add error handling to the following API call :</p>
  <pre class="lang-js prettyprint-override"><code>useEffect(() =&gt; {
          const fetchData = async () =&gt; {
              setLoading(true);
              const data =  await axios.get(`https://realtor.p.rapidapi.com/properties/v2/list-for-rent?sort=relevance&amp;city=Miami&amp;state_code=FL&amp;limit=100&amp;offset=0`, headers);
  
              const properties = data.data.properties;
              console.log(properties);
  
              /// Iterates through data and grabs all the data for house listings
              const listings =  properties.map((listing, index) =&gt; {
                  const arr = [];
                  arr.push(
                      listing.listing_id,
                      listing.address.line, 
                      listing.address.city, 
                      listing.address.county, 
                      listing.address.neighborhood_name, 
                      listing.address.state,
                      listing.year_built,
                      listing.address.lat,
                      listing.address.lon);
  
                  arr.push(listing.photos.map((photo) =&gt; {
                      return photo.href;
                  }));
  
                  return arr;
              });
  
              // House listing data is put into houseData
              //getHouseData(listings);
              setListings(listings);
              setLoading(false);
          }
          fetchData();
      }, [])
  </code></pre>