🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

react-variable-height-infinite-scroller

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-variable-height-infinite-scroller

variable row height scroller that renders just the visible rows with no precomputation of row-height necessary

2.0.6
Source
npm
Version published
Weekly downloads
32
-60%
Maintainers
1
Weekly downloads
 
Created
Source

react-variable-height-infinite-scroller

An infinite scroller especially made for variable row heights (no precomputation of row height necessary).

See a Demo

Why

Because sometimes you don't know the size of the rows you're going to render before rendering

Install:

npm i --save react-variable-height-infinite-scroller

Usage:

Props

NameDefaultDescription
averageElementHeight numberrequiredThis is a guess you make of what is the average height. This is used to approximate number of rows when rendering more or less rows
containerHeight numberrequiredMaximum height of the scroll container
preloadRowStart numberrequiredIf you want to start at a particular row to begin with
totalNumberOfRows numberrequiredLength of the data array
renderRow functionrequiredFunction to render a row
rowToJumpTo(optional)Object of shape { row: Number }. Row you want to jump to. Must be passed as a new object each time to allow for difference checking
containerClassName string(optional) infiniteContainerclassName to apply on container
onScroll function(optional) no-opHook to call on scroll

Taken from the demo code:

import React from 'react';
import InfiniteScroller from './InfiniteScroller.js';

function getFakeRowsWithHeights(numberOfRows) {
  let newFakeRows = [];
  for (let i = 0; i < numberOfRows; i++) {
    newFakeRows.push({height: Math.floor(1000 * Math.random())});
  }
  return newFakeRows;
}

const App = React.createClass({
  getNewRandomRow(totalRows) {
    return {row: Math.floor(totalRows * Math.random())};
  },

  getInitialState() {
    return {
      rowToJumpTo: null,
      newRowToJumpTo: this.getNewRandomRow(100),
      fakeRows: getFakeRowsWithHeights(100),
    };
  },
  render() {
    const newNumberOfRowsToDisplay = Math.floor(Math.random() * 200);
    return (
      <div overflow="scroll">
        <button onClick={() => {
          this.setState({
            rowToJumpTo: this.state.newRowToJumpTo,
            newRowToJumpTo: this.getNewRandomRow(this.state.fakeRows.length),
          });
        }}>
          Jump to a random row: Row #{this.state.newRowToJumpTo.row} (its height is {this.state.fakeRows[this.state.newRowToJumpTo.row].height})
        </button>
        <button onClick={() => {
          this.setState({
            fakeRows: getFakeRowsWithHeights(newNumberOfRowsToDisplay),
          });
        }}>
          Create {newNumberOfRowsToDisplay} new rows
        </button>
        <InfiniteScroller
          averageElementHeight={100} // this is a guess you make!
          containerHeight={600}
          rowToJumpTo={this.state.rowToJumpTo} // (optional) row you want to jump to. Must be passed as a new object each time to allow for difference checking
          renderRow={renderRow} // function to render a row
          totalNumberOfRows={this.state.fakeRows.length} // an array of data for your rows
          preloadRowStart={10} // if you want to start at a particular row to begin with
        />
      </div>
    );
  },

  renderRow(rowNumber) {
    const heightOfRow = this.state.fakeRows[rowNumber].height;
    return (
      <div
        key={rowNumber}
        style={{height: heightOfRow, background: heightOfRow % 2 === 0 ? 'red' : 'orange'}}
      >
        {heightOfRow}
      </div>
    );
  },
});

React.render(<App />, document.getElementById('container'));

Contributing

Changelog is now autogenerated. So commits have to be prefixed by one the four following prefixes:

  • [added] added a new feature
  • [changed] changed an existing feature
  • [fixed] fixed a bug
  • [removed] removed something or a file

Run npm test to lint

#Changelog: Changelog

Keywords

react

FAQs

Package last updated on 04 Sep 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts