axios vs fetch

ARJUN KUMAR DAS
Oct 24, 2020
const [countries, setCountries] = useState([]);
const [cart, setCart] = useState([]);
// Load Data with fetch;useEffect(() =>{
const url = 'https://restcountries.eu/rest/v2/all'
fetch(url)
.then(res => res.json())
.then(data =>setCountries(data))
.catch(error => console.log(error))
},[]);
// Load Data with axios;
useEffect
(() =>{
const url = 'https://restcountries.eu/rest/v2/all'
axios(url)
.then(data => setCountries(data.data.countries))
},[]);

Axios has url in request object. Axios is a stand-alone third party package that can be easily installed. Axios uses the data property. Axios has wide browser support. Axios performs automatic transforms of JSON data.

Fetch has no url in request object. Fetch is built into most modern browsers; no installation is required as such. Fetch is a two-step process when handling JSON data- first, to make the actual request; second, to call the .json() method on the response.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

ARJUN KUMAR DAS
ARJUN KUMAR DAS

Written by ARJUN KUMAR DAS

0 Followers

Front_end Web Developer

No responses yet

Write a response