Member-only story
Top React Hooks — Scroll and Breakpoints
3 min readOct 10, 2020
Hooks contains our logic code in our React app.
We can create our own hooks and use hooks provided by other people.
In this article, we’ll look at some useful React hooks.
react-use
The react-use library is a big library with many handy hooks.
The useScrolling
hook lets us keep track of whether the user is scrolling an element or not.
To use it, we can write:
import React from "react";
import { useScrolling } from "react-use";export default function App() {
const scrollRef = React.useRef(null);
const scrolling = useScrolling(scrollRef); return (
<div ref={scrollRef} style={{ overflow: "scroll", height: 300 }}>
<div style={{ position: "fixed" }}>
{scrolling ? "Scrolling" : "Not scrolling"}
</div>
{Array(1000)
.fill()
.map((_, i) => (
<p key={i}>{i}</p>
))}
</div>
);
}
We create the ref that we pass into the element that we want to watch the scrolling for.
Also, we pass the ref to the element to the useScrolling
hook.
The hook returns the scrolling state of the element.