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

Investigate scroll event debouncing #27

Open
kripod opened this issue Sep 21, 2019 · 2 comments
Open

Investigate scroll event debouncing #27

kripod opened this issue Sep 21, 2019 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@kripod
Copy link
Owner

kripod commented Sep 21, 2019

Motivation

Scroll events may be fired at a high frequency, without taking unwanted framerate drops into account.

Details

A similar problem is present for virtual scrolling/windowing approaches. We should follow the thread below for further information:

@kripod kripod added enhancement New feature or request help wanted Extra attention is needed labels Sep 21, 2019
@kripod
Copy link
Owner Author

kripod commented Oct 25, 2019

Issue #113 could provide a partial solution:

function useDebouncedWindowScrollCoords() {
  const coords = useWindowScrollCoords();
  const [debouncedCoords, setDebouncedCoords] = useState(coords);

  const isChanging = useChanging(coords, 150);
  useEffect(() => {
    if (!isChanging) setDebouncedCoords(coords);
  }, [isChanging]);

  return debouncedCoords;
}

The main drawback is that useWindowScrollCoords still features useState under the hood, which would cause unnecessary rerenders.

@kripod
Copy link
Owner Author

kripod commented Oct 25, 2019

The code above could be made generic:

function useDebounce<T>(value: T, groupingIntervalMs?: number) {
  const [debouncedValue, setDebouncedValue] = useState(value);

  const isChanging = useChanging(value, groupingIntervalMs);
  useEffect(() => {
    if (!isChanging) setDebouncedValue(value);
  }, [isChanging]);

  return debouncedValue;
}

After all, rerenders are not so problematic, especially with some room for additional optimization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant