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(hooks): optimize useScrollPosition with useCallback and useRef #3049

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lucky-cobras-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/use-scroll-position": patch
---

WHAT: Refactored the useScrollPosition hook to improve performance and stability by using useCallback for the handler function and useRef for throttleTimeout.
23 changes: 14 additions & 9 deletions packages/hooks/use-scroll-position/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {useRef, useEffect} from "react";
import {useRef, useEffect, useCallback} from "react";

const isBrowser = typeof window !== "undefined";

export type ScrollValue = {x: any; y: any};
export type ScrollValue = {x: number; y: number};

function getScrollPosition(element: HTMLElement | undefined | null): ScrollValue {
if (!isBrowser) return {x: 0, y: 0};
Expand Down Expand Up @@ -41,26 +41,26 @@ export const useScrollPosition = (props: UseScrollPositionOptions): ScrollValue
isEnabled ? getScrollPosition(elementRef?.current) : {x: 0, y: 0},
);

let throttleTimeout: ReturnType<typeof setTimeout> | null = null;
const throttleTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);

const handler = () => {
const handler = useCallback(() => {
const currPos = getScrollPosition(elementRef?.current);

if (typeof callback === "function") {
callback({prevPos: position.current, currPos});
}

position.current = currPos;
throttleTimeout = null;
};
throttleTimeout.current = null;
}, [callback, elementRef]);

useEffect(() => {
if (!isEnabled) return;
jrgarciadev marked this conversation as resolved.
Show resolved Hide resolved

const handleScroll = () => {
if (delay) {
if (throttleTimeout === null) {
throttleTimeout = setTimeout(handler, delay);
if (throttleTimeout.current === null) {
throttleTimeout.current = setTimeout(handler, delay);
}
} else {
handler();
Expand All @@ -71,7 +71,12 @@ export const useScrollPosition = (props: UseScrollPositionOptions): ScrollValue

target.addEventListener("scroll", handleScroll);

return () => target.removeEventListener("scroll", handleScroll);
return () => {
target.removeEventListener("scroll", handleScroll);
if (throttleTimeout.current) {
clearTimeout(throttleTimeout.current);
}
};
}, [elementRef?.current, delay, isEnabled]);

jrgarciadev marked this conversation as resolved.
Show resolved Hide resolved
return position.current;
Expand Down
Loading