I built an api that returns me the units that I have registered in a DB but when trying to return these are not reviewing
import React, { Component } from 'react';
import axios from 'axios';
const api = {
units: () => {
axios.get('http://192.168.0.23/api/public/api/units')
.then(response => {
console.log(response);
return response.data.data
}).catch((error) => {
console.log(error.message)
});
return 'Error'
},
};
export default api;
The response displays the data correctly, the response.data.data
if used in a file with a class I can set a state units = []
and with a setState
return the units
However I would like to create this file for the api returns, however as I do not use a class so I understand I can not use the state.
Is there any way of without the class returning this data, or keeping in a variable or something of the sort?
Or use setState
right here without a class?
I believe that these would be the ways to solve my problem, but if someone else knows otherwise than putting the api call in the same file as the final class may be too.
I tried that way too:
async function getUnits() {
return await axios.get('http://192.168.0.23/api/public/api/units');
}
getUnits.then((response) => {
console.log(response);
return response.data.data
}).catch((response) => {
console.log(response)
});
But the error indicated that getUnits.then is not a function
Even with a normal function did not work:
function units() {
axios.get('http://192.168.0.23/api/public/api/units')
.then(response => {
console.log(response);
return response.data.data
}).catch((error) => {
console.log(error.message)
});
return 'Error'
};