Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
react-list
Advanced tools
The react-list npm package is a versatile library for efficiently rendering large lists and tabular data in React applications. It provides a simple API to handle infinite scrolling, virtualized lists, and lazy loading, which helps in improving performance by only rendering the visible items.
Simple List Rendering
This feature allows you to render a simple list of items. The `itemRenderer` function is used to define how each item should be rendered, and the `length` prop specifies the number of items in the list.
import React from 'react';
import ReactList from 'react-list';
class SimpleList extends React.Component {
renderItem(index, key) {
return <div key={key}>Item {index}</div>;
}
render() {
return (
<ReactList
itemRenderer={this.renderItem.bind(this)}
length={1000}
type='uniform'
/>
);
}
}
export default SimpleList;
Infinite Scrolling
This feature demonstrates how to implement infinite scrolling. The `loadMoreItems` function is called when the user scrolls to the bottom of the list, adding more items to the state.
import React from 'react';
import ReactList from 'react-list';
class InfiniteScrollList extends React.Component {
constructor(props) {
super(props);
this.state = { items: Array.from({ length: 20 }) };
}
loadMoreItems() {
this.setState((prevState) => ({
items: prevState.items.concat(Array.from({ length: 20 }))
}));
}
renderItem(index, key) {
return <div key={key}>Item {index}</div>;
}
render() {
return (
<div onScroll={(e) => {
if (e.target.scrollHeight - e.target.scrollTop === e.target.clientHeight) {
this.loadMoreItems();
}
}}>
<ReactList
itemRenderer={this.renderItem.bind(this)}
length={this.state.items.length}
type='uniform'
/>
</div>
);
}
}
export default InfiniteScrollList;
Variable Height Items
This feature allows you to render a list with items of variable heights. The `type` prop is set to 'variable', and the `itemRenderer` function generates items with random heights.
import React from 'react';
import ReactList from 'react-list';
class VariableHeightList extends React.Component {
renderItem(index, key) {
const height = 30 + Math.random() * 100;
return <div key={key} style={{ height }}>{`Item ${index} (height: ${height}px)`}</div>;
}
render() {
return (
<ReactList
itemRenderer={this.renderItem.bind(this)}
length={100}
type='variable'
/>
);
}
}
export default VariableHeightList;
react-virtualized is a library for efficiently rendering large lists and tabular data. It offers a wide range of components and utilities for handling virtualized lists, grids, and tables. Compared to react-list, react-virtualized provides more flexibility and customization options but has a steeper learning curve.
react-window is a lightweight library for rendering large lists and tabular data. It is designed to be simpler and more efficient than react-virtualized, with a smaller bundle size. While it offers fewer features than react-virtualized, it is easier to use and integrates well with modern React applications.
react-infinite-scroller is a library focused on implementing infinite scrolling in React applications. It provides a simple API for loading more items as the user scrolls. Compared to react-list, it is more specialized for infinite scrolling but does not offer the same level of flexibility for other types of list rendering.
A versatile infinite scroll React component.
bower install react-list
# or
npm install react-list
ReactList depends on React.
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>
);
}
}
y
)The axis that this list scrolls on.
An index to scroll to after mounting.
A function that receives an index and a key and returns the content to be rendered for the item at that index.
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.
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.
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
.
0
)The number of items in the list.
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.
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.
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.
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.
100
)The number of pixels to buffer at the beginning and end of the rendered list items.
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.
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.
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.
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.
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.
[firstIndex, lastIndex]
Return the indices of the first and last items that are at all visible in the viewport.
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.
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.
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>
open docs/index.html
make
FAQs
A versatile infinite scroll [React] component.
The npm package react-list receives a total of 259,931 weekly downloads. As such, react-list popularity was classified as popular.
We found that react-list demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.