Skip to content

Commit 5b39ab5

Browse files
committed
Type checking + minor improvements useScroll
1 parent eab6c51 commit 5b39ab5

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

docs/useScroll.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
# `useScroll`
22

3-
React sensor hook that re-renders on when scroll position in a DOM element changes.
3+
React sensor hook that re-renders when the scroll position in a DOM element changes.
44

55
## Usage
66

77
```jsx
88
import {useScroll} from 'react-use';
99

1010
const Demo = () => {
11-
const element = React.useRef(null);
12-
const {x, y} = useScroll(element);
11+
const scrollRef = React.useRef(null);
12+
const {x, y} = useScroll(scrollRef);
1313

1414
return (
15-
<div ref={element}>
15+
<div ref={scrollRef}>
1616
<div>x: {x}</div>
1717
<div>y: {y}</div>
1818
</div>
1919
);
2020
};
2121
```
22+
23+
## Reference
24+
25+
```ts
26+
useScroll(ref: RefObject<HTMLElement>);
27+
```

src/__stories__/useScroll.story.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import {useScroll} from '..';
44
import ShowDocs from './util/ShowDocs';
55

66
const Demo = () => {
7-
const element = React.useRef(null);
8-
const {x, y} = useScroll(element);
7+
const scrollRef = React.useRef(null);
8+
const {x, y} = useScroll(scrollRef);
99

1010
return (
1111
<>
1212
<div>x: {x}</div>
1313
<div>y: {y}</div>
1414
<div
15-
ref={element}
15+
ref={scrollRef}
1616
style={{
1717
width: '400px',
1818
height: '400px',
@@ -30,6 +30,4 @@ const Demo = () => {
3030

3131
storiesOf('Sensors/useScroll', module)
3232
.add('Docs', () => <ShowDocs md={require('../../docs/useScroll.md')} />)
33-
.add('Demo', () =>
34-
<Demo/>
35-
)
33+
.add('Demo', () => <Demo/>)

src/useScroll.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
1-
import {useState, useEffect, useRef} from 'react';
2-
import {isClient} from './util';
1+
import {useState, useEffect, useRef, RefObject} from 'react';
32

43
export interface State {
54
x: number;
65
y: number;
76
}
87

9-
const useScroll = (ref): State => {
8+
const useScroll = (ref: RefObject<HTMLElement>): State => {
9+
if (process.env.NODE_ENV === 'development') {
10+
if ((typeof ref !== 'object') || (typeof ref.current === 'undefined')) {
11+
console.error('`useScroll` expects a single ref argument.');
12+
}
13+
}
14+
1015
const frame = useRef(0);
1116
const [state, setState] = useState<State>({
12-
x: isClient ? window.scrollX : 0,
13-
y: isClient ? window.scrollY : 0
17+
x: 0,
18+
y: 0
1419
});
1520

1621
useEffect(() => {
1722
const handler = () => {
1823
cancelAnimationFrame(frame.current)
24+
1925
frame.current = requestAnimationFrame(() => {
20-
setState({
21-
x: ref.current.scrollLeft,
22-
y: ref.current.scrollTop
23-
});
26+
if (ref.current) {
27+
setState({
28+
x: ref.current.scrollLeft,
29+
y: ref.current.scrollTop
30+
})
31+
}
2432
});
2533
}
2634

27-
if (ref && ref.current) {
35+
if (ref.current) {
2836
ref.current.addEventListener('scroll', handler, {
2937
capture: false,
3038
passive: true
@@ -36,11 +44,11 @@ const useScroll = (ref): State => {
3644
cancelAnimationFrame(frame.current);
3745
}
3846

39-
if (ref && ref.current) {
47+
if (ref.current) {
4048
ref.current.removeEventListener('scroll', handler);
4149
}
4250
};
43-
}, [ref]);
51+
}, [ref.current]);
4452

4553
return state;
4654
}

0 commit comments

Comments
 (0)