Fetch Data In React Application

Photo by Lautaro Andreani on Unsplash

There are so many methods that we can use to fetch data for an API. Some of them are Fetch API, Axios, Superagent, Request and so on.

Fetch API — The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses. It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network.

fetch(‘http://example.com/movies.json')

.then(response => response.json())

.then(data => console.log(data));

Axios — Promise based HTTP client for the browser and node.js

const axios = require(‘axios’)

axios.get(‘/user?ID=12345’)

.then(function (response) { // handle success console.log(response); })

.catch(function (error) { // handle error console.log(error); })

.then(function () { // always executed });

Axios npm package.

You can check on other methods as well. If you want to dig deeper please follow the official documentations.

Thanks for reading.

--

--