• Stars
    star
    1,951
  • Rank 23,754 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“œ A versatile infinite scroll React component.

ReactList

A versatile infinite scroll React component.

Install

bower install react-list

# or

npm install react-list

ReactList depends on React.

Examples

Check out the example page and the the example page source for examples of different configurations.

Here's another simple example to get you started.

import loadAccount from 'my-account-loader';
import React from 'react';
import ReactList from 'react-list';

class MyComponent extends React.Component {
  state = {
    accounts: []
  };

  componentWillMount() {
    loadAccounts(::this.handleAccounts);
  }

  handleAccounts(accounts) {
    this.setState({accounts});
  }

  renderItem(index, key) {
    return <div key={key}>{this.state.accounts[index].name}</div>;
  }

  render() {
    return (
      <div>
        <h1>Accounts</h1>
        <div style={{overflow: 'auto', maxHeight: 400}}>
          <ReactList
            itemRenderer={::this.renderItem}
            length={this.state.accounts.length}
            type='uniform'
          />
        </div>
      </div>
    );
  }
}

Props

axis (defaults to y)

The axis that this list scrolls on.

initialIndex

An index to scroll to after mounting.

itemRenderer(index, key)

A function that receives an index and a key and returns the content to be rendered for the item at that index.

itemsRenderer(items, ref)

A function that receives the rendered items and a ref. By default this element is just a <div>. Generally it only needs to be overridden for use in a <table> or other special case. NOTE: You must set ref={ref} on the component that contains the items so the correct item sizing calculations can be made.

itemSizeEstimator(index, cache)

A function that receives an item index and the cached known item sizes and returns an estimated size (height for y-axis lists and width for x-axis lists) of that item at that index. This prop is only used when the prop type is set to variable and itemSizeGetter is not defined. Use this property when you can't know the exact size of an item before rendering it, but want it to take up space in the list regardless.

itemSizeGetter(index)

A function that receives an item index and returns the size (height for y-axis lists and width for x-axis lists) of that item at that index. This prop is only used when the prop type is set to variable.

length (defaults to 0)

The number of items in the list.

minSize (defaults to 1)

The minimum number of items to render at any given time. This can be used to render some amount of items initially when rendering HTML on the server.

pageSize (defaults to 10)

The number of items to batch up for new renders. Does not apply to 'uniform' lists as the optimal number of items is calculated automatically.

scrollParentGetter (defaults to finding the nearest scrollable parent)

A function that returns a DOM Element or Window that will be treated as the scrolling container for the list. In most cases this does not need to be set for the list to work as intended. It is exposed as a prop for more complicated uses where the scrolling container may not initially have an overflow property that enables scrolling.

scrollParentViewportSizeGetter (defaults to scrollParent's viewport size)

A function that returns the size of the scrollParent's viewport. Provide this prop if you can efficiently determine your scrollParent's viewport size as it can improve performance.

threshold (defaults to 100)

The number of pixels to buffer at the beginning and end of the rendered list items.

type (one of simple, variable, or uniform, defaults to simple)
  • simple This type is...simple. It will not cache item sizes or remove items that are above the viewport. This type is sufficient for many cases when the only requirement is incremental rendering when scrolling.

  • variable This type is preferred when the sizes of the items in the list vary. Supply the itemSizeGetter when possible so the entire length of the list can be established beforehand. Otherwise, the item sizes will be cached as they are rendered so that items that are above the viewport can be removed as the list is scrolled.

  • uniform This type is preferred when you can guarantee all of your elements will be the same size. The advantage here is that the size of the entire list can be calculated ahead of time and only enough items to fill the viewport ever need to be drawn. The size of the first item will be used to infer the size of every other item. Multiple items per row are also supported with this type.

useStaticSize (defaults to false)

Set to true if the item size will never change (as a result of responsive layout changing or otherwise). This prop is only used when the prop type is set to uniform. This is an opt-in optimization that will cause the very first element's size to be used for all elements for the duration of the component's life.

useTranslate3d (defaults to false)

A boolean to determine whether the translate3d CSS property should be used for positioning instead of the default translate. This can help performance on mobile devices, but is supported by fewer browsers.

Methods

scrollTo(index)

Put the element at index at the top of the viewport. Note that if you aren't using type='uniform' or an itemSizeGetter, you will only be able to scroll to an element that has already been rendered.

scrollAround(index)

Scroll the viewport so that the element at index is visible, but not necessarily at the top. The scrollTo note above also applies to this method.

getVisibleRange() => [firstIndex, lastIndex]

Return the indices of the first and last items that are at all visible in the viewport.

FAQ

What is "ReactList failed to reach a stable state."?

This happens when specifying the uniform type without actually providing uniform size elements. The component attempts to draw only the minimum necessary elements at one time and that minimum element calculation is based off the first element in the list. When the first element does not match the other elements, the calculation will be wrong and the component will never be able to fully resolve the ideal necessary elements.

Why doesn't it work with margins?

The calculations to figure out element positioning and size get significantly more complicated with margins, so they are not supported. Use a transparent border or padding or an element with nested elements to achieve the desired spacing.

Why is there no onScroll event handler?

If you need an onScroll handler, just add the handler to the div wrapping your ReactList component:

<div style={{height: 300, overflow: 'auto'}} onScroll={this.handleScroll}>
  <ReactList ... />
</div>

Development

open docs/index.html
make

More Repositories

1

watchy

Run commands when paths change.
JavaScript
125
star
2

cursors

Maintain your React state with Cursors.
JavaScript
80
star
3

CoffeeScript.mode

CoffeeScript syntax highlighting for Coda 2
44
star
4

cogs

The fast file transform pipeline.
JavaScript
31
star
5

formatted-text

A React component for formatting paragraphs, line breaks and links.
JavaScript
12
star
6

backbone-composite-keys

Composite key support for Backbone. Just set your `idAttribute` to an array of attributes and you're all set.
CoffeeScript
10
star
7

pave

Paving the way to better state management.
JavaScript
9
star
8

my-homebridge

My Homebridge set up with Docker. You can use it too!
Shell
6
star
9

herit

Easy to use JavaScript "Class" inheritance with support for "instance" and "static" properties.
JavaScript
5
star
10

blackmailr

Get what you want, when you want it, or else...
Ruby
5
star
11

amdainty

A small, simple AMD module resolver for use in single-file, AMD-style projects.
JavaScript
4
star
12

backbone-relations

Backbone one-to-one, one-to-many, and many-to-many relationships for the browser and nodejs.
JavaScript
4
star
13

underscore-express

Use Underscore templates easily in Express.
JavaScript
3
star
14

bob

Bob builds Docker images.
JavaScript
3
star
15

react-selectorer

A fully customizable React selector component.
JavaScript
3
star
16

guard-mirror

A CoffeeScript, Stylus, and Jade (HTML and JST) Guard that mirrors your source files (.coffee/.styl/.jst.jade/.html.jade) in another location (public/www/etc...).
Ruby
3
star
17

turbo-car-club

Welcome to The Club.
JavaScript
2
star
18

docdock

JavaScript
2
star
19

bruteball

Roll into stuff.
JavaScript
2
star
20

backbone-collection-crud

Add `save` and `destroy` to your collections.
CoffeeScript
2
star
21

borer

Bores HTTP tunnels
JavaScript
2
star
22

sock-band

Socket.io + Instruments = Sock Band
JavaScript
2
star
23

anagram-war

It's a war of the words. Uses Node, Backbone, Underscore, jQuery, Express, Connect, Socket.io, Zappa, and anything else you can dream of.
JavaScript
2
star
24

homekit-proxy

Proxy various non-HomeKit devices as HomeKit devices.
JavaScript
2
star
25

fort-knox

Get secrets from Vault
JavaScript
1
star
26

nfsevents

Proxy file events from one machine to another, useful for inotify over NFS.
JavaScript
1
star
27

cogs-transformer-terser

A terser transformer for Cogs.
JavaScript
1
star
28

nodekick

NKO 2013 Team Us Entry
JavaScript
1
star
29

dpr

Trying to make life easier in a world with too many device pixel ratios.
JavaScript
1
star
30

jobby

YET ANOTHER REDIS JOB QUEUE!
JavaScript
1
star
31

jquery-cwd

jQuery plugins by caseyWebDev
CoffeeScript
1
star
32

consul-lb

A Consul-aware load balancer powered by nginx and secured by Let's Encrypt.
Shell
1
star
33

bumpit

Easily bump versions for your package.json, bower.json, and/or component.json from the command line.
JavaScript
1
star
34

live-socket

WebSocket wrapper to add socket.io-like functionality around native WebSockets or polyfills.
JavaScript
1
star
35

eslint-config-coderiety

ESLint configuration used by Coderiety projects.
JavaScript
1
star
36

bootman

Start (and stop) services dependent on one another in order
JavaScript
1
star
37

docker-postgres

A containerpilot-wrapped postgres image.
Shell
1
star
38

caseywebdev.github.io

caseyWebDev GitHub Site
JavaScript
1
star
39

kiss-test

Keep It Simple, Stupid - Test
JavaScript
1
star
40

interaction

An interaction-style "class" for Node.js
JavaScript
1
star
41

jquery-mobile-events

A component for just the jquery-mobile events.
Makefile
1
star
42

extrascore

An extension for Underscore.js with some of the tools I reuse all the time.
CoffeeScript
1
star
43

cogs-transformer-marked

A marked transformer for Cogs.
JavaScript
1
star
44

containerpilot-consul

The official Consul image wrapped in ContainerPilot.
Shell
1
star