Last active
November 28, 2018 00:17
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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