-1
I built an api that returns the units I have registered in a DB but when trying to return these not passing
import React, { Component } from 'react';
import axios from 'axios';
const api = {
let: units = [],
units: () => {
axios.get('http://192.168.0.23/apiTeste/public/api/unidades')
.then(response => {
console.log(response);
return response.data.data
}).catch((error) => {
console.log(error.message)
});
return 'Falha ao tentar retornar as unidades'
},
};
export default api;
The report presents the data correctly, the response.data.data
if I use in a file with a class I can define a state units = []
and with a setState
return the units
However I would like to create this file for the returns of the api, but as I do not use a class so I understand I can not use state.
Have some way without the class return this data, or save in a variable or something like that?
Or else use the setState
right here without a class?
Or last case build a class to be returned in another class that will call the function? Ex:
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View } from 'react-native';
import api from './units';
export default class App extends Component {
render() {
return (
<View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Units: {api.units()}</Text>
</View>
);
}
}
I believe these would be the ways to solve my problem, but if someone knows otherwise than to put the call from the api in the same file as the final class it might be too.
What if in the
App
I don’t have a class? There’s no way to return because I wouldn’t have astate
, or would have some way?– Mateus
Then you need to explain better what you need, so I understand now Oce wants to update the
state
ofApp
from a call onapi.units
somewhere else that doesn’t have state, like inside any other file? That’s it ?– Neuber Oliveira
Not what I want is to receive these api values without using state, so you can receive the data even in a file where you are not using a class
– Mateus
Okay, so the code I passed works like this in the comments
//result vai ser o "response.data.data" que passou para o resolve la em units
, the api data will be inside thethen
, studies a little about Precedents that will help you understand a lot of the Act thing– Neuber Oliveira