Add Async/Await to React Fetch Request

I’m making a fetch request to this API and I’m successfully getting the data and printing it to the console. However I’m new to asynchronous calls in Javascript/React. How do add async/await in this code to delay the render upon successful fetch? I’m getting the classic Uncaught (in promise) TypeError: Cannot read property '0' of undefined because I believe that the DOM is trying to render the data that hasn’t been fully fetched yet.

import React, { useEffect, useState } from "react";

export default function News() {
  const [error, setError] = useState(null);
  const [isLoaded, setIsLoaded] = useState(false);
  const [stories, setStory] = useState([]);

  useEffect(() => {
    fetch(
      "http://api.mediastack.com/v1/news"
    )
      .then((res) => res.json())
      .then(
        (result) => {
          setIsLoaded(true);
          setStory(result);
          console.log(result.data[0]);       // printing to console to test response
          console.log(result.data[0].title); // printing to console to test response
        },
        (error) => {
          setIsLoaded(true);
          setError(error);
        }
      );
  }, []);

  if (error) {
    return <div>Error: {error.message}</div>;
  } else if (!isLoaded) {
    return <div>Loading...</div>;
  } else {
    return (
      <div>
        <p>{stories.data[0].title} </p> // this is the where render error is
      </div>
    );
  }
}

147 thoughts on “Add Async/Await to React Fetch Request”

  1. Hi there I am so glad I found your blog page, I really found
    you by error, while I was browsing on Yahoo for something else, Anyhow I am here now and would just like to say many thanks for
    a fantastic post and a all round entertaining blog (I also love the theme/design), I don’t have time
    to read through it all at the minute but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read
    a lot more, Please do keep up the awesome jo. http://antiibioticsland.com/Cephalexin.htm

    Reply
  2. Pingback: priligy toxicity
  3. Pingback: ivermectinsales
  4. It’s appropriate time to make some plans for the future and it’s time to be
    happy. I’ve read this post and if I could I wish to suggest you few interesting things or suggestions.
    Perhaps you could write next articles referring to this article.
    I wish to read more things about it!

    Reply
  5. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my
    newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

    Reply
  6. Have you ever thought about including a little bit more than just your articles?

    I mean, what you say is important and all.
    Nevertheless imagine if you added some great pictures or
    video clips to give your posts more, “pop”! Your content
    is excellent but with images and videos, this website could undeniably be one of the most beneficial in its niche.
    Amazing blog!

    Reply
  7. We stumbled over here different web page and thought I should check things out.

    I like what I see so i am just following you. Look forward to
    looking at your web page repeatedly.

    Reply
  8. I have read a few just right stuff here. Certainly value bookmarking for revisiting.
    I wonder how so much effort you place to create such a wonderful informative website.

    Reply
  9. Hi there! Someone in my Facebook group shared
    this website with us so I came to give it a look. I’m definitely enjoying the information. I’m book-marking and
    will be tweeting this to my followers! Wonderful blog and fantastic
    design.

    Reply

Leave a Comment