I have a question concerning the display of a list in reactJS (with Next.JS).
This is the usual way I display a list, using such code :
theList.map((e:MyType) =>
<div key={e.orderNbr}>
<DisplayOneListElement .... />
</div>)
The list is then shown starting with its first element and it works fine. But assuming the list has 300 elements long, how would I change the code so the user would see the list starting with its 191st element ?
While keeping the possibility to scroll up or down later on.
191 is of course arbitrary.
2
Answers
You want to render the subset of the list efficiently. You may think of using libraries like
react-window
orreact-virtualised
if feasible.Otherwise, you will have to write logic for showing a certain subset of list at a time. Here is what you can use as a base –
I have noted your latest comment that you have resolved the case. Still the below post may be useful to those seeking similar information.
As we know, for scrolling functionality, we need to "step out" of React and communicate with the external API. React provides a better way to do this through Ref. DOM nodes can be manipulated through Ref. When React updates the DOM, it will populate Ref with DOM node and will make the node accessible from the code.
Please see below a sample code which implements a similar functionality you have been asking for. It is based on Ref.
Note: In case you need to manipulate a list of DOM nodes, please see this sample code: How to manage a list of refs using a ref callback
App.js
Test run:
On loading the app, there will be 99 list items displayed, and an automatic scrolling will be performed by the code to the item indexed at 99. The items on top will still be accessible through scrolling.