Skip to content

Commit

Permalink
Merge pull request #5 from jaredpalmer/jaredpalmer-patch-1
Browse files Browse the repository at this point in the history
Update readme with proper TS signatures
  • Loading branch information
jaredpalmer committed Sep 27, 2018
2 parents 352affe + 774f09a commit 149e801
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions README.md
Expand Up @@ -10,7 +10,13 @@ npm install react-simple-infinite-scroll --save

## Usage

### Basic
This component uses a "sentinel" `div` (with a React ref) that calls `getBoundingClientRect()` to measure its position and fire off a callback when it becomes visible again. Note: this package eventually becomes somewhat inefficient on very very very large lists because it keeps adding nodes to the DOM. However, this package is extremely valuable for situations when a windowing technique is not possible and when a user is going to realistically scroll a few hundred rows (and _not_ thousands of rows).

### Why not use windowing (`react-virtualized`)?

If you can, you probably should. However, windowing only works when you know the total number of items in the result set ahead of time. This isn't always possible. For example, let's say you have a MongoDB database where you cannot efficiently return the total number of documents in a given query. All your API returns is a cursor (so you can know is if there is another page or not). While this would prevent you from using windowing/`react-virtualized`, `react-simple-infinite-scroll` will work just fine.

### The Gist

```jsx
<InfiniteScroll
Expand All @@ -20,7 +26,7 @@ npm install react-simple-infinite-scroll --save
hasMore={!!this.state.cursor}
onLoadMore={this.loadMore}
>
{this.state.entities}
{this.state.myArrayOfItems.map(item => <div>{/* ... */}</div>)}
</InfiniteScroll>
```

Expand All @@ -33,7 +39,7 @@ npm install react-simple-infinite-scroll --save
onLoadMore={this.loadMore}
render={({ sentinel }) => (
<section>
{this.state.entities}
{this.state.myArrayOfItems.map(item => <div>{/* ... */}</div>)}
{sentinel}
</section>
)}
Expand Down Expand Up @@ -123,42 +129,55 @@ export class MyInfiniteScrollExample extends React.Component {
}
```

## Props / Options

### hasMore
## API Reference

### Props

#### `hasMore: boolean`

**[boolean|required]** Specifies if there are more entities to load.
**Required**

### isLoading
Specifies if there are more entities to load.

**[boolean|required]** When true, `onLoadMore()` will not be executed on scroll.
#### `isLoading: boolean`

### onLoadMore
**Required**

**[function|required]** Called when the user has scrolled all the way to the end. This happens when the `sentinel` has reached the `threshold`.
When `true`, `onLoadMore()` will not be executed on scroll.

### threshold
#### `onLoadMore: () => void`

**[number|default:100]** Scroll threshold. Number of pixels before the `sentinel` reaches the viewport to trigger `onLoadMore()`.
**Required**

### throttle
Called when the user has scrolled all the way to the end. This happens when the `sentinel` has reached the `threshold`.

**[number|default:64]** Scroll handler will be executed at most once per the number of milliseconds specified.
#### `threshold?: number`

Scroll threshold. Number of pixels before the `sentinel` reaches the viewport to trigger `onLoadMore()`.

#### `throttle?: number = 64`

Defaults to `64`. Scroll handler will be executed at most once per the number of milliseconds specified.

**Warning:** Making this number closer to zero can decrease performance due to a force reflow caused by `getBoundingClientRect()`, see more properties that can cause this issue in [this gist by Paul Irish](https://gist.github.com/paulirish/5d52fb081b3570c81e3a).

### render
#### `render?: (props: ScrollProps) => React.ReactNode`

**[function|optional]** Callback used for convenient inline rendering and wrapping. Arguments passed `Object: { sentinel, children }`. Use this if you have a more complex layout where the `sentinel` needs to be injected.
Callback used for convenient inline rendering and wrapping. Arguments passed `Object: { sentinel, children }`. Use this if you have a more complex layout where the `sentinel` needs to be injected.

**Warning:** The `sentinel` must be rendered (injected into the DOM) in order for this library to work properly, failing to do so will result in errors and unexpected side effects.

### component
#### `component?: React.ComponentType<ScrollProps>`

**[component|optional]** React component. Similar to the `render()` prop, this component will receive `Object: { sentinel, children }` as props. **Note** that `render()` prop has precedence over this property, meaning that if both are present, `component` will not be rendered.
React component. Similar to the `render()` prop, this component will receive `Object: { sentinel, children }` as props. **Note** that `render()` prop has precedence over this property, meaning that if both are present, `component` will not be rendered.

**Warning:** The `sentinel` must be rendered (injected into the DOM) in order for this library to work properly, failing to do so will result in errors and unexpected side effects.

## Alternatives

- [`brigade/react-waypoint`](https://github.com/brigade/react-waypoint)

## Author

* Jared Palmer [@jaredpalmer](https://twitter.com/jaredpalmer)
Expand Down

0 comments on commit 149e801

Please sign in to comment.