1
I’m starting in the world of Reactjs. I have a front that is consuming a tracking API, so it returns me an object with status, responseMessage and Object, an array of objects. I do the unstructuration of the request and it performs the print normally on the console. But the moment I try to change the state, it does not accuse any error but also does not change the state of the component.
const dispatch = useDispatch();
const [dados, setDados] = React.useState([]);
React.useEffect(() => {
async function loadData(){
await dispatch(signInRequestMultiportal());
const response = await apiMultiportal.request({
url: 'posicoes/ultimaPosicao',
method: 'post',
headers: {
token: store.getState().auth.tokenMultiportal,
}
});
const { object } = response.data;
console.log(object)
setDados(object);
console.log(dados);
}
loadData();
}, [])
{
"status": 'OK',
"responseMessage": "Requisição concluida",
"object": [{
}]
}
Print of the object, the top is the console.log(object)
the bottom the console.log(dados)
if you are saying that you do not also change on the screen or only on the console.log?
console.log(dados);
?:– novic
In the.log console the data appears, but in useState it does not
– LUCAS MICHEL DA SILVA
your Object ta coming anything, like an array with 0 positions! if you saw this?
– novic
In the
console.log(object)
all data appears, but inconsole.log(dados)
simply appears a [ ]– LUCAS MICHEL DA SILVA
if you are confusing the balls, the console.log(data) has no data yet it takes a while to update following the example below that I will create
– novic
It worked, thank you very much!
– LUCAS MICHEL DA SILVA
if you have been successful please tick as an answer to your question
– novic