I am fetching an API and assigning its data to an object by using setArticles. Problem is, I am mapping on "articles.map()" this data in Functional component and get an error, because state articles have null value before fetching data. Please see screenshot attached if I am not making sense, sorry for that.
Issue is, component gets rendered first, and at that render time, "articles" have no value, and I am assigning it a value after fetching.
Question is
- How can I fetch data and use it in a component?
Top comments (3)
There several things we need to discuss here
The correct version maybe look like:
OMG
I finally achieved it.
Here is a screenshot attached with right way.
Thanks for your reply
Unfortunately, nothing helped.
you declared a const "articles" 2 times.
Once in state, second, after fetch...