Skip to content

Commit

Permalink
fix(useViewportRange): don't update state after unmount (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusNotheis authored Apr 3, 2020
1 parent 7aab047 commit 6b8daef
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions packages/base/src/hooks/useViewportRange.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Device } from '@ui5/webcomponents-react-base/lib/Device';
import { useCallback, useEffect, useState } from 'react';
import { useEffect, useState, useRef } from 'react';

export const useViewportRange = (rangeSet) => {
const [currentRange, setCurrentRange] = useState(Device.media.getCurrentRange(rangeSet, window.innerWidth).name);

const onWindowResize = useCallback(
({ name: range }) => {
setCurrentRange(range);
},
[setCurrentRange]
);
const isMounted = useRef(true);

useEffect(() => {
Device.media.attachHandler(onWindowResize, null, 'StdExt');
const handler = ({ name: range }) => {
if (isMounted.current === true) {
setCurrentRange(range);
}
};
Device.media.attachHandler(handler, null, 'StdExt');
return () => {
Device.resize.detachHandler(onWindowResize, null);
isMounted.current = false;
Device.resize.detachHandler(handler, null);
};
}, [onWindowResize]);
}, [setCurrentRange, isMounted]);

return currentRange;
};

0 comments on commit 6b8daef

Please sign in to comment.