-1

I'm trying to make a fetch request for COVID data in my React Native app but each time I try to inspect the response, the console outputs undefined for the json variable:

  const [isLoading, setLoading] = useState(true);
  const [data, setData] = useState({});

  useEffect(() => {
    fetch("https://api.covid19api.com/summary")
      .then((response) => {
        response.json();
      })
      .then((json) => {
        console.log("json.. " + json);
        setData(json);
      }) 
      .catch((error) => console.error(error))
      .finally(() => setLoading(false));
  }, []);
BobSacamano
  • 157
  • 1
  • 8

1 Answers1

1

In the first .then(), you are not returning anything, so undefined is returned implicitly.

You should return the reponse.json():

.then((response) => {
   return response.json();
})

Or shorter:

.then((response) => response.json())
Matias Kinnunen
  • 7,828
  • 3
  • 35
  • 46