1
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 )
2
2
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.
4
4
5
5
### Window
6
6
7
7
![ Example] ( ./example.gif )
8
8
9
-
10
- ### Container
9
+ ### Container
11
10
12
11
![ Example] ( ./example_inner.gif )
13
12
@@ -21,25 +20,71 @@ yarn:
21
20
22
21
## Usage
23
22
24
- ### On bottom of entire screen
23
+ ### Hook
25
24
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
27
31
32
+ ``` jsx
33
+ import { useBottomScrollListener } from ' react-bottom-scroll-listener' ;
34
+
35
+ useBottomScrollListener (callback);
28
36
```
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
+
29
74
< BottomScrollListener onBottom= {callback} / >
30
75
```
31
76
32
- ### On bottom of specific container
77
+ #### On bottom of specific container
33
78
34
79
Pass the BottomScrollListener a function inside the JSX_tag, receive the ` scrollRef ` in this function from the BottomScrollListener
35
80
and pass it to the component you want to listen for a scroll event on.
36
81
37
- ```
82
+ ``` jsx
83
+ import BottomScrollListener from ' react-bottom-scroll-listener' ;
84
+
38
85
< BottomScrollListener onBottom= {callback}>
39
86
{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>
43
88
)}
44
89
< / BottomScrollListener>
45
90
```
0 commit comments