I'm new to react js and have been building something which works absolutely fine. But as of now the api calls to be made are defined inside respective components. So if any changes to be made to the ip or endpoint, i need to make changes at every place. How can i separate all the api calls in one file and perform the operation on result in respective component file.
Below is an example :
import axios from "axios";
function get_all_user(){
axios({
method: "get",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
url: "http://127.0.0.1:8000/app/users/",
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
})
}
function get_single_user(userid){
axios({
method: "get",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
url: "http://127.0.0.1:8000/app/users/"+userid,
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
})
}
in every component i have api calls to make. all these api calls needs to be at one place. Any help is appreciated.