There is a problem There is a class for working with API
export default class RickAndMortyApi {
api = axios.create({
baseURL: 'https://rickandmortyapi.com/api',
});
getCharacters() {
this.api.get('/character/?page=1')
.then(response => {
return response.data.results; //array with data. Everything works correctly
})
}
}
In this class, I am getting a response from the server. Everything works correctly. But when I want to get this data in a component, I get an undefined
Component:
useEffect(() => {
charactersRequested();
const data = rickAndMortyApi.getCharacters();
console.log(data); //undefind
charactersLoaded(data);
}, []);