React Hooks Fetch Data

import React, { useState, useEffect } from “react”;

const App = () =>

{

const [hasError, setErrors] = useState(false);

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

async function fetchData()

{

const response = await fetch(“http://ergast.com/api/f1/2004/1/results.json");

response

.json()

.then(res => setData(res))

.catch(err => setErrors(err));

}

useEffect(() => {

fetchData();

});

return (

<div>

<span>{JSON.stringify(data)}</span>

<hr />

<span>Has error: {JSON.stringify(hasError)}</span>

</div>

);

};

export default App;

--

--

--

Full Stack Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Gatsby Page Queries with Environment Variables

Adding Web Workers To The Static Site Created With React And Python

Javascript behind the scenes

Task:- 7.2

Critical CSS and Webpack: Automatically Minimize Render-Blocking CSS

Clone A Graph

clone Graph Example Illustration

10 Inspiring Ideas For Your Next Front-End Project

Simple GraphQL Server with NodeJS and Express

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
ilhan DEMİRTEPE

ilhan DEMİRTEPE

Full Stack Developer

More from Medium

What is this React Hooks

How to fetch data from an API in ReactJS ?