File tree 1 file changed +11
-11
lines changed 1 file changed +11
-11
lines changed Original file line number Diff line number Diff line change 1
1
import { Device } from '@ui5/webcomponents-react-base/lib/Device' ;
2
- import { useCallback , useEffect , useState } from 'react' ;
2
+ import { useEffect , useState , useRef } from 'react' ;
3
3
4
4
export const useViewportRange = ( rangeSet ) => {
5
5
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 ) ;
13
7
14
8
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' ) ;
16
15
return ( ) => {
17
- Device . resize . detachHandler ( onWindowResize , null ) ;
16
+ isMounted . current = false ;
17
+ Device . resize . detachHandler ( handler , null ) ;
18
18
} ;
19
- } , [ onWindowResize ] ) ;
19
+ } , [ setCurrentRange , isMounted ] ) ;
20
20
21
21
return currentRange ;
22
22
} ;
You can’t perform that action at this time.
0 commit comments