Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Update React hook useScroll #71

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

KentHHarris
Copy link

@KentHHarris KentHHarris commented Oct 18, 2023

Description

The React hook useScroll can incorrectly reflect the initial scrolled state. This happens on first render, when the user's starting y-position is greater than the threshold. The state reports false when it should say true.

Here, you can see we've started with a vertical scroll position beyond the threshold. As such, the Navbar should have a slightly opaque background; however, it's completely transparent.

Screenshot 2023-10-18 at 3 36 51 PM

Solution

One way to fix this is by checking the user's initial y-position on first mount. We do that by invoking the onScroll handler when also registering it as a scroll event listener. That way, we don't rely on a scrolled event to correctly update the state. Note: it will always render as false on the server, but corrects itself immediately when the client first renders the component.

Here, we see our solution implemented and Navbar has the background we'd expect.

Screenshot 2023-10-18 at 3 37 09 PM

@vercel
Copy link

vercel bot commented Oct 18, 2023

@KentHHarris is attempting to deploy a commit to the Elegance Team on Vercel.

A member of the Team first needs to authorize it.

@KentHHarris KentHHarris changed the title Update React hook useScroll [Fix] Update React hook useScroll Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant