how to fetch data from an Api with React

Asked

Viewed 19 times

-1

I am trying to display properties of an api with React but get the following error: inserir a descrição da imagem aqui

notice that on the right the property exists is I call it below: inserir a descrição da imagem aqui

  • what is the return layout? have an example? please put code in place of these two images!

1 answer

0

What happens is that a call to the API is asynchronous, so when your component is rendering for the first time it has not yet received the result of the API so that the object does not exist, hence the error. You didn’t enter the call code, but I’m guessing there’s a car service. With this you can put {cars.show.nameBrand || ''} then it will not show anything until you receive a value, then when the status update with the value received by the API will show the car name correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.