Skip to content

Commit c0b42ca

Browse files
committed
feat(components): adding scroll to top
1 parent 6b9b259 commit c0b42ca

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/components/ScrollToTop.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,36 @@ import { BsArrowUp } from 'react-icons/bs';
55
function ScrollToTop() {
66
const [isVisible, setIsVisible] = useState(false);
77

8-
const toggleVisibility = useCallback(() => {
9-
if (window.pageYOffset > 100) {
10-
setIsVisible(true);
11-
} else {
12-
setIsVisible(false);
13-
}
14-
});
8+
const toggleVisibility = useCallback(() => {
9+
if (window.pageYOffset > 100) {
10+
setIsVisible(true);
11+
} else {
12+
setIsVisible(false);
13+
}
14+
}, []);
1515

1616
const backToTop = useCallback(() => {
1717
window.scrollTo({ top: 0, behavior: 'smooth' });
1818
}, []);
1919

20-
useEffect(() => {
21-
window.addEventListener('scroll', toggleVisibility);
20+
useEffect(() => {
21+
window.addEventListener('scroll', toggleVisibility);
2222

23-
return () => {
24-
window.removeEventListener('scroll', toggleVisibility);
25-
};
26-
}, []);
23+
return () => {
24+
window.removeEventListener('scroll', toggleVisibility);
25+
};
26+
}, [toggleVisibility]);
2727

2828
return (
29-
<button className={`btn btn-sm fixed bottom-12 right-8 ${isVisible ? null : 'hidden'}`} onClick={() => backToTop()}>
30-
<BsArrowUp />
31-
</button>
32-
)
29+
<button
30+
className={`btn btn-sm fixed bottom-12 right-8 ${
31+
isVisible ? null : 'hidden'
32+
}`}
33+
onClick={() => backToTop()}
34+
>
35+
<BsArrowUp />
36+
</button>
37+
);
3338
}
3439

3540
export default ScrollToTop;

0 commit comments

Comments
 (0)