Skip to content

Commit 4ec24a8

Browse files
committed
Update documentation to cover "scroll in specific container" feature
1 parent 36cf654 commit 4ec24a8

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

README.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# react-bottom-scroll-listener [![NPM version](https://img.shields.io/npm/v/react-bottom-scroll-listener.svg?style=flat)](https://www.npmjs.com/package/react-bottom-scroll-listener) [![npm bundle size (minified)](https://img.shields.io/bundlephobia/minzip/react-bottom-scroll-listener.svg)](https://github.com/karl-run/react-bottom-scroll-listener)
1+
# react-bottom-scroll-listener [![NPM version](https://img.shields.io/npm/v/react-bottom-scroll-listener.svg?style=flat)](https://www.npmjs.com/package/react-bottom-scroll-listener) [![npm bundle size (minified)](https://img.shields.io/bundlephobia/minzip/react-bottom-scroll-listener.svg)](https://github.com/karl-run/react-bottom-scroll-listener)
22

33
A simple React component that lets you listen for when you have scrolled to the bottom.
44

@@ -14,12 +14,40 @@ yarn:
1414

1515
## Usage
1616

17+
### On bottom of entire screen
18+
19+
Simply have the BottomScrollListener anywhere in your application and pass it a function as `onBottom`-prop.
20+
1721
```
1822
<BottomScrollListener onBottom={callback} />
1923
```
20-
__Props__
2124

22-
* `onBottom` __(required):__ callback invoked when bottom is reached
23-
* `debounce`: _(default: 200)_ integer in ms, how much debounce there should be on callback
24-
* `offset`: _(default: 0)_ offset from bottom in pixels. E.g. 300 if it should invoke `onBottom` 300px before the bottom.
25-
* `children`: _(default: null)_ Not required, but you can use this to wrap your components. Most useful when you have some conditional rendering.
25+
### On bottom of specific container
26+
27+
Pass the BottomScrollListener a function inside the JSX_tag, receive the `ref` in this function from the BottomScrollListener
28+
and pass it to the component you want to listen for a scroll event on.
29+
30+
```
31+
<BottomScrollListener onBottom={callback}>
32+
{ref => (
33+
<div ref={ref}>
34+
Callback will be invoked when this container is scrolled to bottom.
35+
</div>
36+
)}
37+
</BottomScrollListener>
38+
```
39+
40+
> Those are some weird children, what's going on?
41+
42+
This pattern is called "function as a child". What this allows is that the BottomScrollListener can pass you a `React.RefObject`. This
43+
`React.RefObject` can then be passed to whatever component you want to be notified when you hit the bottom of. Without this it would be
44+
difficult to attach event listeners for scrolling to an arbitrary element.
45+
46+
**Props**
47+
48+
| Property | Type | Default | Description |
49+
| -------- | :----------------------: | :-----: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
50+
| onBottom | Function | null | **(required):** callback invoked when bottom is reached |
51+
| debounce | number | 200 | milliseconds, how much debounce there should be on the callback |
52+
| offset | number | 0 | offset from bottom in pixels. E.g. 300 if it should invoke `onBottom` 300px before the bottom. |
53+
| children | React.Node _or_ Function | null | Not required, but you can use this to wrap your components. Most useful when you have some conditional rendering. If this is a function, that function will receive a React.RefObject that _needs_ to be passed to a child element. This element will then be used as the scroll container. |

example/src/App.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ export default class App extends Component {
3939
/>
4040
<div>Show alert on bottom</div>
4141
</label>
42-
<BottomScrollListener
43-
onBottom={this.handleContainerOnBottom}
44-
>
42+
<BottomScrollListener onBottom={this.handleContainerOnBottom}>
4543
{ref => (
4644
<div ref={ref} className="innerScrollExample">
4745
<h4>Callback when this container hits bottom</h4>

0 commit comments

Comments
 (0)