mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
Update tests/useWindowScroll.test.tsx Co-authored-by: Mathias <mathiassoeholm@gmail.com> fix for useWindowScroll may lose window scroll change at mount #1699, fixes for review by mathiassoeholm Update tests/useWindowScroll.test.tsx Co-authored-by: Mathias <mathiassoeholm@gmail.com>
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import { useEffect } from 'react';
|
|
|
|
import { isBrowser, off, on } from './misc/util';
|
|
import useRafState from './useRafState';
|
|
|
|
export interface State {
|
|
x: number;
|
|
y: number;
|
|
}
|
|
|
|
const useWindowScroll = (): State => {
|
|
const [state, setState] = useRafState<State>(() => ({
|
|
x: isBrowser ? window.pageXOffset : 0,
|
|
y: isBrowser ? window.pageYOffset : 0,
|
|
}));
|
|
|
|
useEffect(() => {
|
|
const handler = () => {
|
|
setState((state) => {
|
|
const { pageXOffset, pageYOffset } = window;
|
|
//Check state for change, return same state if no change happened to prevent rerender
|
|
//(see useState/setState documentation). useState/setState is used internally in useRafState/setState.
|
|
return state.x !== pageXOffset || state.y !== pageYOffset
|
|
? {
|
|
x: pageXOffset,
|
|
y: pageYOffset,
|
|
}
|
|
: state;
|
|
});
|
|
};
|
|
|
|
//We have to update window scroll at mount, before subscription.
|
|
//Window scroll may be changed between render and effect handler.
|
|
handler();
|
|
|
|
on(window, 'scroll', handler, {
|
|
capture: false,
|
|
passive: true,
|
|
});
|
|
|
|
return () => {
|
|
off(window, 'scroll', handler);
|
|
};
|
|
}, []);
|
|
|
|
return state;
|
|
};
|
|
|
|
export default useWindowScroll;
|