Skip to content

Commit 6b8daef

Browse files
fix(useViewportRange): don't update state after unmount (#402)
1 parent 7aab047 commit 6b8daef

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { Device } from '@ui5/webcomponents-react-base/lib/Device';
2-
import { useCallback, useEffect, useState } from 'react';
2+
import { useEffect, useState, useRef } from 'react';
33

44
export const useViewportRange = (rangeSet) => {
55
const [currentRange, setCurrentRange] = useState(Device.media.getCurrentRange(rangeSet, window.innerWidth).name);
6-
7-
const onWindowResize = useCallback(
8-
({ name: range }) => {
9-
setCurrentRange(range);
10-
},
11-
[setCurrentRange]
12-
);
6+
const isMounted = useRef(true);
137

148
useEffect(() => {
15-
Device.media.attachHandler(onWindowResize, null, 'StdExt');
9+
const handler = ({ name: range }) => {
10+
if (isMounted.current === true) {
11+
setCurrentRange(range);
12+
}
13+
};
14+
Device.media.attachHandler(handler, null, 'StdExt');
1615
return () => {
17-
Device.resize.detachHandler(onWindowResize, null);
16+
isMounted.current = false;
17+
Device.resize.detachHandler(handler, null);
1818
};
19-
}, [onWindowResize]);
19+
}, [setCurrentRange, isMounted]);
2020

2121
return currentRange;
2222
};

0 commit comments

Comments
 (0)