Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite scrolling with React JS

I am looking at ways to implement infinite scrolling with React. I have come across react-infinite-scroll and found it inefficient as it just adds nodes to the DOM and doesn't remove them. Is there any proven solution with React which will add, remove and maintains constant number of nodes in the DOM.

Here is the jsfiddle problem. In this problem, i want to have only 50 elements in the DOM at a time. others should be loaded and removed as user scrolls up and down. We have started using React because of it's optimization algorithms. Now i couldn't find solution to this problem. I have come across airbnb infinite js. But it is implemented with Jquery. To use this airbnb infinite scroll, i have to loose the React optimisation which i don't want to do.

sample code i want to add scroll is(here i am loading all items. My goal is to load only 50 items at a time)

/** @jsx React.DOM */  var Hello = React.createClass({     render: function() {         return (<li>Hello {this.props.name}</li>);     } });  var HelloList = React.createClass({       getInitialState: function() {                                      var numbers =  [];          for(var i=1;i<10000;i++){              numbers.push(i);          }          return {data:numbers};      },      render: function(){        var response =  this.state.data.map(function(contact){                     return (<Hello name="World"></Hello>);         });          return (<ul>{response}</ul>)     } });  React.renderComponent(<HelloList/>, document.getElementById('content')); 

Looking for help...

like image 751
Rajeev Avatar asked Jan 20 '14 16:01

Rajeev


People also ask

How do you make infinite scroll in React?

To implement infinite scroll, the first thing we should know is the last element of the current list. For getting it, we can take help from the onScroll method and save our reference in react useRef. Following code will give us an idea regarding implementation of the onScroll method and using ref.

How do I scroll in React JS?

Scroll to an Element With the Element. scrollIntoView() Method in React: This Method accept one argument. The scrollIntoView() method scrolls an element into the visible area of the browser window.

Is infinite scroll lazy loading?

Infinite scroll uses lazy loading and executes its demand to load more data (products or content) at the bottom of the page, without requiring an action such as the click of a button. On-demand loading is used to optimize content delivery by reducing time consumption and memory usage.


1 Answers

Basically when scrolling you want to decide which elements are visible and then rerender to display only those elements, with a single spacer element on top and bottom to represent the offscreen elements.

Vjeux made a fiddle here which you can look at: jsfiddle.

Upon scrolling it executes

scrollState: function(scroll) {     var visibleStart = Math.floor(scroll / this.state.recordHeight);     var visibleEnd = Math.min(visibleStart + this.state.recordsPerBody, this.state.total - 1);      var displayStart = Math.max(0, Math.floor(scroll / this.state.recordHeight) - this.state.recordsPerBody * 1.5);     var displayEnd = Math.min(displayStart + 4 * this.state.recordsPerBody, this.state.total - 1);      this.setState({         visibleStart: visibleStart,         visibleEnd: visibleEnd,         displayStart: displayStart,         displayEnd: displayEnd,         scroll: scroll     }); }, 

and then the render function will display only the rows in the range displayStart..displayEnd.

You may also be interested in ReactJS: Modeling Bi-Directional Infinite Scrolling.

like image 74
Sophie Alpert Avatar answered Sep 24 '22 05:09

Sophie Alpert