source: imaps-frontend/node_modules/react-use/esm/useWindowScroll.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.4 KB
Line 
1import { useEffect } from 'react';
2import { isBrowser, off, on } from './misc/util';
3import useRafState from './useRafState';
4var useWindowScroll = function () {
5 var _a = useRafState(function () { return ({
6 x: isBrowser ? window.pageXOffset : 0,
7 y: isBrowser ? window.pageYOffset : 0,
8 }); }), state = _a[0], setState = _a[1];
9 useEffect(function () {
10 var handler = function () {
11 setState(function (state) {
12 var pageXOffset = window.pageXOffset, pageYOffset = window.pageYOffset;
13 //Check state for change, return same state if no change happened to prevent rerender
14 //(see useState/setState documentation). useState/setState is used internally in useRafState/setState.
15 return state.x !== pageXOffset || state.y !== pageYOffset
16 ? {
17 x: pageXOffset,
18 y: pageYOffset,
19 }
20 : state;
21 });
22 };
23 //We have to update window scroll at mount, before subscription.
24 //Window scroll may be changed between render and effect handler.
25 handler();
26 on(window, 'scroll', handler, {
27 capture: false,
28 passive: true,
29 });
30 return function () {
31 off(window, 'scroll', handler);
32 };
33 }, []);
34 return state;
35};
36export default useWindowScroll;
Note: See TracBrowser for help on using the repository browser.