From the course: React Hooks

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Fetching data with useEffect

Fetching data with useEffect - React.js Tutorial

From the course: React Hooks

Start my 1-month free trial

Fetching data with useEffect

- [Instructor] Another use case for useEffect is for data fetching. So let's say we had an API like api.github.com/users so this is just a straight up rest API. It returned some Json data and we want to make a request for this data and then load it into our page. So the way I can do that is by using a combination of useState and useEffect. The first thing we'll do is we will update our component quite a bit and let's just start literally from scratch with this thing. So we'll still import useState and useEffect but inside of this app component now we want to create a state variable for data and setData. And then on first render we want useState the initial state just to be an empty array. So then we're going to say return a paragraph that says No Users. So now if we look at our app, it says No Users. It's going to be up to us to get the data from that API using a useEffect call, useEffect always takes in a function…

Contents