Skip to content

Instantly share code, notes, and snippets.

@shobhit-jain
Last active July 7, 2021 20:39
Show Gist options
  • Save shobhit-jain/76e539c0ca0109e5137ebe5697624944 to your computer and use it in GitHub Desktop.
Save shobhit-jain/76e539c0ca0109e5137ebe5697624944 to your computer and use it in GitHub Desktop.
Next.js - Detecting Scrolling Direction - Up / Down
import React from 'react'
import { useScrollDirection } from 'react-use-scroll-direction'
export const Window_Scroll_Direction = () => {
const [direction, setDirection] = React.useState(String)
const { isScrollingUp, isScrollingDown } = useScrollDirection()
React.useEffect(() => {
isScrollingDown && setDirection('down')
isScrollingUp && setDirection('up')
}, [isScrollingDown, isScrollingUp])
return (
<>
<div className="fixed top-0 bg-white">
{direction === 'down' ? 'Scrolling down' : 'scrolling up'}
</div>
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment