DEV Community

Discussion on: How To Use Axios in an Optimized and Scalable Way With React

Collapse
 
alexstaroselsky profile image
Info Comment hidden by post author - thread only visible in this permalink
Alexander Staroselsky • Edited

What is the need for then(response => response) on each method? Did you intend to extract data from the response so the consumer doesn't have to grab data each time? Such as return axiosClient.get(/${URL}).then(response => response.data);? Otherwise it could be removed to shorten those wrapper/helper methods.

Collapse
 
nilanth profile image
Nilanth

To access the response like below

import { getRequest } from 'axiosClient';

async function fetchUser() {
try {
  const user = await getRequest('users');
  console.log(user.data);
} catch(error) {
   //Log errors
  }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
alexstaroselsky profile image
Alexander Staroselsky

What happens when you remove .then(response => response) from getRequest(URL). Does it change how const user = await getRequest('users'); works at all?

Some comments have been hidden by the post's author - find out more