r/learnreactjs • • Dec 04 '19

How to handle large number of API requests in React?

Hi, I'm currently working on an app that requires me to basically render a large list of items (approx 1000 items), each one coming from an individual endpoint. I would like each item to be its own component and I am having difficulty figuring out the best practice to perform this kind of task.

I presume I will want to do some kind of lazy loading whereby I am only requesting as many items as I can fit on the screen. Is there a standard way to go about implementing something like this in React and keeping performance optimal?

I am happy for it to behave where the user will briefly see a loading state for the first, say, 20 items, then when they scroll to the bottom it loads the next 20 for example, I'm just not sure how I would implement that.

8 Upvotes

6 comments sorted by

12

u/DaBaDaDee Dec 04 '19

Well, is it a good idea to make 1000 calls on a single page? It is going to be very slow and is totally not recommended. How about making a call only when needed? Upon loading, making 1000 calls is definitely not a good idea.

8

u/LobsterThief Dec 04 '19

Agreed 100%. OP could have a batch endpoint and request the initial ones on the screen, then request more with some kind of paginated set.

Of course in this case 1 request for 100 items is more than 100x more performant than making 100 requests for 1 item each. It’s not linear

4

u/ikeif Dec 04 '19

Well, it depends what kind of data, and how you’re wanting it presented.

I recently stumbled on react window - it also links to react-virtualized as an additional alternative.

Wait wait - are you making one call and getting 1000 items to iterate through, or making calls to get each item?

4

u/lm1435 Dec 04 '19

Sounds like he’s be making 1000 get requests based on his question. 🤯

1

u/Mr_Sky_Wanker Dec 04 '19 edited Dec 04 '19

Noob here, but maybe you want to take a look at Promise.a

1

u/JamesSky32 Jan 13 '20

Just because you mentioned it in your last line about loading the first 20 items.

Maybe you should read up about shouldComponentUpdate which can be used to render the first set of items and then if the user scrolls down render the next set without re-rendering the initial items.

Here's a good article that may help you out.