简体   繁体   中英

Get updated data in useEffect

I try to fetch data using useEffect . Trying this I get many renders in the fetching process.

Ex:

First render:

data = []

Second render:

data = [{...}{...}]

So the data array is populated just after 2 renders. Why? and how to get data rendered in the first render?

When you are getting your data in useEffect there are going to be extra renders when the data has not been loaded yet. But generally you should not be worried by this as renders are pretty fast.

Only way to get around this is to load the data in some parent component and then pass it to the child component as param. This way you can have a component with non-trivial render of data, that will be displayed by parent only when data are already available

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM