Skip to content
Advertisement

React set a state to the result of an API call

I’m making a call to a getTime function which returns the datetime, but for some reason the state I specify is not being updated, am I misunderstanding how this works? Do I need to await the result?

JavaScript

Advertisement

Answer

Yes, exactly, it’s a fetch, so you gotta wait for the result and set the state only then, so you could do smth like:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement