react-use/src/useWindowScroll.ts
Victor Tiurin 6b708c880d fix for useWindowScroll may lose window scroll change at mount #1699
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>
2021-03-11 10:50:54 +02:00

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;