-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
useWindowScroll may lose window scroll change at mount #1699
Comments
eaglus
added a commit
to eaglus/react-use
that referenced
this issue
Dec 11, 2020
eaglus
added a commit
to eaglus/react-use
that referenced
this issue
Dec 11, 2020
9 tasks
eaglus
added a commit
to eaglus/react-use
that referenced
this issue
Jan 13, 2021
…ich#1699, fixes for review by mathiassoeholm
eaglus
added a commit
to eaglus/react-use
that referenced
this issue
Mar 11, 2021
…ich#1699 Update tests/useWindowScroll.test.tsx Co-authored-by: Mathias <mathiassoeholm@gmail.com> fix for useWindowScroll may lose window scroll change at mount streamich#1699, fixes for review by mathiassoeholm Update tests/useWindowScroll.test.tsx Co-authored-by: Mathias <mathiassoeholm@gmail.com>
xobotyi
added a commit
that referenced
this issue
Mar 11, 2021
fix for useWindowScroll may lose window scroll change at mount #1699
This was referenced Mar 11, 2021
Merged
kamranayub
pushed a commit
to kamranayub/react-use
that referenced
this issue
May 28, 2021
…ich#1699 Update tests/useWindowScroll.test.tsx Co-authored-by: Mathias <mathiassoeholm@gmail.com> fix for useWindowScroll may lose window scroll change at mount streamich#1699, fixes for review by mathiassoeholm Update tests/useWindowScroll.test.tsx Co-authored-by: Mathias <mathiassoeholm@gmail.com>
Merged
4 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the current behavior?
Now, useWindowScroll asks for window scroll position at two points:
But, react render may be async, so, between first render and execution of useEffect may happen a "time gap", and window scroll position may be updated there (in that "gap").
For example, some other effects (of other components may cause window scroll).
And that updated value of window scroll will be lost, becase window.addEventListener is called before that possible update.
We need to update window scroll inside of effect handler, before subscription.
Also, we can use lazy initial value for state (to prevent scroll position recalculation at each render).
It is legal, see there: https://kentcdodds.com/blog/use-state-lazy-initialization-and-function-updates
Steps to reproduce it and if possible a minimal demo of the problem
This is very unstable bug, it depends on heighbor/parent/child configuration, and i can reproduce it on my application (when some route gets mounted, and window scrolled to top, useWindowScroll gets remounted, but, its new instance has wrong scroll position value).
What is the expected behavior?
Always have correct hook result, correspondent to window scroll position
A little about versions:
react-use
: 15.3.4The text was updated successfully, but these errors were encountered: