Fetch Data from API with useEffect in React JS | React JS | EP - 8

Опубликовано: 09 Ноябрь 2024
на канале: JAS ACADAMY
507
11

This video will quickly go over how to make use of the useEffect hook in React to retrieve data from an API.

This video assumes that you have a general understanding of how to fetch/retrieve data from an API as well as the fundamentals of React and React Hooks.

The component will be responsible for displaying an image of a random dog that is received from the API that we are fetching from. To do this, we'll need to:

Import useState and useEffect
Create our dogImage variable as well as the setDogImage function via useState
Create out useEffect function — this is where we'll perform our fetch
Within our useEffect function we'll use setDogImage to.. well... set dogImage to the image url that we received
Use dogImage as the src for our image so that we can display the random dog

The API that we're using has a few different endpoints that we can use, but for this simple example we'll just use the random image endpoint. This endpoint will simply return an object containing a message key and status key, we're only concerned about the message key in this simple example. The message key will contain the url that we'll use as the source for our image.




#reactjs #reactutorial #fetch #reactjstraining
#jasacadamy