Skip to content

Commit a042187

Browse files
committed
chore: update hooks documentation
1 parent 91e8712 commit a042187

File tree

6 files changed

+69
-23
lines changed

6 files changed

+69
-23
lines changed

README.md

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# 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

3-
A simple React component that lets you listen for when you have scrolled to the bottom.
3+
A simple **React hook** and **React component** that lets you listen for when you have scrolled to the bottom.
44

55
### Window
66

77
![Example](./example.gif)
88

9-
10-
### Container
9+
### Container
1110

1211
![Example](./example_inner.gif)
1312

@@ -21,25 +20,71 @@ yarn:
2120

2221
## Usage
2322

24-
### On bottom of entire screen
23+
### Hook
2524

26-
Simply have the BottomScrollListener anywhere in your application and pass it a function as `onBottom`-prop.
25+
[Full example](/example/src/HookExample.js)
26+
27+
#### On bottom of entire screen
28+
29+
Use the hook in any functional component, the callback will be invoked
30+
when the user scrolls to the bottom of the document
2731

32+
```jsx
33+
import { useBottomScrollListener } from 'react-bottom-scroll-listener';
34+
35+
useBottomScrollListener(callback);
2836
```
37+
38+
#### On bottom of specific container
39+
40+
Use the hook in any functional component, use the ref given from the hook
41+
and pass it to the element you want to use as a scroll container
42+
43+
The callback will be invoked when the user scrolls to the bottom of the container
44+
45+
```jsx
46+
import { useBottomScrollListener } from 'react-bottom-scroll-listener';
47+
48+
const containerRef = useBottomScrollListener(callback);
49+
50+
<div ref={scrollRef}>Callback will be invoked when this container is scrolled to bottom.</div>
51+
```
52+
53+
**Parameters**
54+
55+
```
56+
useBottomScrollListener(
57+
onBottom, // Required callback that will be invoked when scrolled to bottom
58+
offset = 0, // Offset from bottom of page in pixels. E.g. 300 will trigger onBottom 300px from the bottom of the page
59+
debounce = 200, // Optional debounce in milliseconds, defaults to 200ms
60+
) // returns React.MutableRefObject Optionally you can use this to pass to a element to use that as the scroll container
61+
```
62+
63+
### Component
64+
65+
[Full example](/example/src/ComponentExample.js)
66+
67+
#### On bottom of entire screen
68+
69+
Simply have the BottomScrollListener anywhere in your application and pass it a function as `onBottom`-prop.
70+
71+
```jsx
72+
import BottomScrollListener from 'react-bottom-scroll-listener';
73+
2974
<BottomScrollListener onBottom={callback} />
3075
```
3176

32-
### On bottom of specific container
77+
#### On bottom of specific container
3378

3479
Pass the BottomScrollListener a function inside the JSX_tag, receive the `scrollRef` in this function from the BottomScrollListener
3580
and pass it to the component you want to listen for a scroll event on.
3681

37-
```
82+
```jsx
83+
import BottomScrollListener from 'react-bottom-scroll-listener';
84+
3885
<BottomScrollListener onBottom={callback}>
3986
{scrollRef => (
40-
<div ref={scrollRef}>
41-
Callback will be invoked when this container is scrolled to bottom.
42-
</div>
87+
<div ref={scrollRef}>Callback will be invoked when this container is scrolled to bottom.</div>
4388
)}
4489
</BottomScrollListener>
4590
```

example/.eslintrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"lint": "eslint src --ext .ts,.tsx",
3838
"build": "rollup -c",
3939
"start": "rollup -c -w",
40-
"prepublishOnly": "tsc && lint && yarn test",
40+
"prepublishOnly": "tsc && yarn lint && yarn test",
4141
"prepare": "yarn run build",
4242
"predeploy": "cd example && yarn install && yarn run build",
4343
"deploy": "gh-pages -d example/build"
@@ -89,6 +89,6 @@
8989
"typescript": "^3.5.2"
9090
},
9191
"resolutions": {
92-
"@types/react": "^16.8.19"
92+
"@types/react": "^16.8.20"
9393
}
9494
}

src/hook/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('useBottomScrollListener', () => {
4242

4343
window.dispatchEvent(new Event('scroll'))
4444

45-
expect(onBottom).toHaveBeenCalledTimes(0)
45+
expect(onBottom).not.toHaveBeenCalled()
4646
})
4747

4848
it('shall invoke onBottom when body is exactly at bottom', async () => {

src/hook/index.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const createCallback = (debounce: number, handleOnScroll: () => void): (() => vo
99
}
1010
}
1111

12+
/**
13+
* A react hook that invokes a callback when user scrolls to the bottom
14+
*
15+
* @param onBottom Required callback that will be invoked when scrolled to bottom
16+
* @param offset Offset from bottom of page in pixels. E.g. 300 will trigger onBottom 300px from the bottom of the page
17+
* @param debounce Optional debounce in milliseconds, defaults to 200ms
18+
* @return React.MutableRefObject Optionally you can use this to pass to a element to use that as the scroll container
19+
*/
1220
function useBottomScrollListener<T extends HTMLElement>(
1321
onBottom: () => void,
1422
offset: number = 0,
@@ -33,6 +41,8 @@ function useBottomScrollListener<T extends HTMLElement>(
3341
onBottom()
3442
}
3543
}
44+
// ref dependency needed for the tests, doesn't matter for normal execution
45+
// eslint-disable-next-line react-hooks/exhaustive-deps
3646
}, [offset, onBottom, containerRef.current])
3747

3848
useEffect((): (() => void) => {

yarn.lock

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,7 @@
272272
dependencies:
273273
"@types/react" "*"
274274

275-
"@types/react@*", "@types/react@^16.8.19":
276-
version "16.8.19"
277-
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.8.19.tgz#629154ef05e2e1985cdde94477deefd823ad9be3"
278-
integrity sha512-QzEzjrd1zFzY9cDlbIiFvdr+YUmefuuRYrPxmkwG0UQv5XF35gFIi7a95m1bNVcFU0VimxSZ5QVGSiBmlggQXQ==
279-
dependencies:
280-
"@types/prop-types" "*"
281-
csstype "^2.2.0"
282-
283-
"@types/react@^16.8.20":
275+
"@types/react@*", "@types/react@^16.8.20":
284276
version "16.8.20"
285277
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.8.20.tgz#4f633ecbd0a4d56d0ccc50fff6f9321bbcd7d583"
286278
integrity sha512-ZLmI+ubSJpfUIlQuULDDrdyuFQORBuGOvNnMue8HeA0GVrAJbWtZQhcBvnBPNRBI/GrfSfrKPFhthzC2SLEtLQ==

0 commit comments

Comments
 (0)