How to fetch data from an API in ReactJS ?
API: API is an abbreviation for Application Programming Interface which is a collection of communication protocols and subroutines used by various programs to communicate between them. A programmer can make use of various API tools to make its program easier and simpler. Also, an API facilitates the programmers with an efficient way to develop their software programs.
Approach: In this article, we will know how we fetch the data from API (Application Programming Interface). For the data, we have used the API endpoint from http://jsonplaceholder.typicode.com/users we have created the component in App.js and styling the component in App.css. From the API we have target “id”, “name”, “username”, “email” and fetch the data from API endpoints. Below is the stepwise implementation of how we fetch the data from an API in react. We will use the fetch function to get the data from the API.
Step by step implementation to fetch data from an api in react.
Step 1: Create React Project
npm create-react-app MY-APP
Step 2: Change your directory and enter your main folder charting as
Step 3: API endpoint
Step 4: Write code in App.js to fetch data from API and we are using fetch function.
Project Structure: It will look the following.
Write code in App.css for styling the app.js file.
Step to run the application: Open the terminal and type the following command.
Output: Open the browser and our project is shown in the URL http://localhost:3000/