This website collects cookies to deliver better user experience
How to Code an API Call with React!
How to Code an API Call with React!
Hey fellow creators
Let's code an API Call with React.
This is pretty common and you need to master it if you want to use React.
If you prefer to watch the video version, it's right here :
1. The hooks you need!
You need to import two hooks:
import{useState, useEffect}from"react";functionApp(){return(<divclassName="app"><h1>React API Call</h1></div>)}exportdefaultApp;
Now create your state which will start at false since at first you don't have any data:
import{useState, useEffect}from"react";functionApp(){const[imgURL, setImgURL]=useState(false);return(<divclassName="app"><h1>React API Call</h1></div>)}exportdefaultApp;
2. Fetch an API
Find a random image API and do a fetch when the component is mounted, using the hook useEffect().
Let's start by simply logging the response of the fetch to check if it's working: