Skip to content

Instantly share code, notes, and snippets.

@stephencweiss
Last active November 28, 2018 00:17
Show Gist options
  • Save stephencweiss/8620e1c2dac5f96745a705ab5e3dccdd to your computer and use it in GitHub Desktop.
Save stephencweiss/8620e1c2dac5f96745a705ab5e3dccdd to your computer and use it in GitHub Desktop.
Refactoring an Axios get method from within a React App to use a baseURL
// The original GET request which worked when the service ran on its own
getData(id){
axios.get(`/data/${id}`)
.then( (response) => {
this.setState({ data : response.data }) })
.catch( (error) => { console.log( `The error of the axios GET is: -------> `, error); })
}
// Refactored GET request to take advantage of an example baseURL
getData(id){
const instance = axios.create({ baseURL:'http://www.thesampleurl.com' });
instance.get(`/data/${id}`)
.then( (response) => {
this.setState({ data : response.data }) })
.catch( (error) => { console.log(`The error of the axios GET is: -------> `, error); })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment