score:59
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.
score:1
import react, { component } from 'react';
import infinitescroll from 'react-infinite-scroller';
const api = {
baseurl: '/joblist'
};
class jobs extends component {
constructor(props) {
super(props);
this.state = {
listdata: [],
hasmoreitems: true,
nexthref: null
};
}
fetchdata(){
var self = this;
var url = api.baseurl;
if(this.state.nexthref) {
url = this.state.nexthref;
}
fetch(url)
.then( (response) => {
return response.json() })
.then( (json) => {
var list = self.state.listdata;
json.data.map(data => {
list.push(data);
});
if(json.next_page_url != null) {
self.setstate({
nexthref: resp.next_page_url,
listdata: list
});
} else {
self.setstate({
hasmoreitems: false
});
}
})
.catch(error => console.log('err ' + error));
}
}
componentdidmount() {
this.fetchdata();
}
render() {
const loader = <div classname="loader">loading ...</div>;
let jobitems;
if(this.state.listdata){
jobitems = this.state.listdata.map(job => {
return (
<tr>
<td>{job.job_number}</td>
<td>{job.title}</td>
<td>{job.description}</td>
<td>{job.status}</td>
</tr>
);
});
}
return (
<div classname="jobs">
<div classname="container">
<h2>jobs list</h2>
<infinitescroll
pagestart={0}
loadmore={this.fetchdata.bind(this)}
hasmore={this.state.hasmoreitems}
loader={loader}>
<table classname="table table-bordered">
<thead>
<tr>
<th>job number</th>
<th>title</th>
<th>description</th>
<th>status</th>
</tr>
</thead>
<tbody>
{jobitems}
</tbody>
</table>
</infinitescroll>
</div>
</div>
);
}
}
export default jobs;
score:26
check out our react infinite library:
https://github.com/seatgeek/react-infinite
update december 2016
i've actually been using react-virtualized in a lot of my projects recently and find that it covers the majority of use cases a lot better. both libraries are good, it depends on exactly what you're looking for. for instance, react-virtualized supports variable height jit measuring via an hoc called cellmeasurer
, example here https://bvaughn.github.io/react-virtualized/#/components/cellmeasurer.
update november 2018
a lot of the lessons from react-virtualized have been ported to the smaller, faster, more efficient react-window library from the same author.
Source: stackoverflow.com
Related Query
- Infinite scrolling with React JS
- How to achieve infinite scrolling in React with variable height elements?
- React - Infinite scrolling - child elemnts with dynamic heights
- Handling React animation with horizontal scrolling
- How react Infinite Queries support with offset and limit
- React Infinite Scroll with qwest
- Manage checkbox state in an infinite scrolling table using React
- React Infinite Scroller - Two children with the same key. loadMore function being called twice
- Scrolling to a position in an element on page load with React useEffect
- React useEffect with property as dependency makes infinite loop
- Need a clearer explanation how to avoid infinite re-rendering with React hooks
- I want rerender when props data change with react hooks but i got infinite rerender
- React Horizontal Scrolling cards with Arrows on top
- React infinite scroll in a container with dynamic height?
- Header flickering and freezing issue with material table (implemented with react window + react window infinite loader)
- Infinite useEffect loop when using Redux with React Hooks
- React Hooks infinite loop with Array
- stop/prevent scrolling to top on re render in React using Hooks (need to implement infinite scrolling)
- Infinite loop when working with react and react-firebase-hooks
- Why does this create an infinite render loop with React hooks?
- React hooks: How to read & update state in hooks without infinite loops with react-hooks/exhaustive-deps rule
- Scrolling components into view with react with useRef
- How to do fetch with React Hooks; ESLint enforcing `exhaustive-deps` rule, which causes infinite loop
- React Virtualized MultiGrid with Infinite Loader
- React Query with hooks is throwing error, "Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."
- React useState with an empty object causes an infinite loop
- React infinite loop in useEffect() with reducer
- How can I fill a konva rect with fillPatternImage in React or create an infinite grid with the same svg image in React
- React Typescript Infinite loop with render from function
- React with Inversify makes useEffect infinite loop
More Query from same tag
- Why did the two seperate chart components squeeze into the same box?
- How to wrap a container inside a map function with conditional in JSX - TypeScript
- Add/remove state array item immutably
- React Search bar fetch api
- How does ReactJS evaluate any JavaScript Expression inside JSX?
- How can I render different data from a single react component?
- Cannot set state of multiple checkboxes?
- JS - Transform an Object into an Array unless it is an array
- React context is returning undefined
- Login form not submitting while using submitFormHandler ReactJS
- React antd steps re-render child component to parent state change
- useAsyncStorage custom hook with useState hook in React Native
- Wordpress Gutenberg Autocompleters, no auto completion completes
- React editing specific item
- Is the call to super(props) in an ES6 class important?
- Reach useRef() Current property return unidentified though using UseEffect to fire when adding a child element inside a Div
- NextJs routing to the same page with different query
- React.js array .push() is taking multiple values
- How can I access the props when the page loads?
- How to test functionality of function props in storybook?
- referencing an amd module(arcgis) in webpack app
- React project WebsocketServer.js SyntaxError on heartbeatInterval
- How do I get the "results" object nested in the array "news" and map over it?
- What are the pros and cons on using React Context API with the useReducer() hook?
- React recompose componentFromStream update not triggers
- Get data from a local API with Redux
- DefaultRoute throws "Cannot call a class as a function"
- React Hook Form Controller Issues
- How to use react-responsive as a const
- React Hooks state update applies only once when called from callbacks in an array of components