diff --git a/src/useAccessibilityInfo.ts b/src/useAccessibilityInfo.ts index 8b33b061..5b90e8b0 100644 --- a/src/useAccessibilityInfo.ts +++ b/src/useAccessibilityInfo.ts @@ -21,9 +21,22 @@ function useAccessibilityStateListener( } AccessibilityInfo[initializerName]().then(setIsEnabled) - AccessibilityInfo.addEventListener(eventName, setIsEnabled) - return () => AccessibilityInfo.removeEventListener(eventName, setIsEnabled) + const subscription = AccessibilityInfo.addEventListener( + eventName, + setIsEnabled, + ) + + return () => { + // @ts-expect-error - React Native >= 0.65 + if (typeof subscription?.remove === 'function') { + // @ts-expect-error - need update @types/react-native@0.65.x + subscription.remove() + } else { + // React Native < 0.65 + AccessibilityInfo.removeEventListener(eventName, setIsEnabled) + } + } }, [eventName, initializerName]) return isEnabled diff --git a/src/useAppState.ts b/src/useAppState.ts index 54d2b9c6..ff1b4281 100644 --- a/src/useAppState.ts +++ b/src/useAppState.ts @@ -10,10 +10,17 @@ export function useAppState() { setAppState(newState) } - AppState.addEventListener('change', onChange) + const subscription = AppState.addEventListener('change', onChange) return () => { - AppState.removeEventListener('change', onChange) + // @ts-expect-error - React Native >= 0.65 + if (typeof subscription?.remove === 'function') { + // @ts-expect-error - need update @types/react-native@0.65.x + subscription.remove() + } else { + // React Native < 0.65 + AppState.removeEventListener('change', onChange) + } } }, []) diff --git a/src/useDeviceOrientation.ts b/src/useDeviceOrientation.ts index 8b46071d..df2c9325 100644 --- a/src/useDeviceOrientation.ts +++ b/src/useDeviceOrientation.ts @@ -21,10 +21,17 @@ export function useDeviceOrientation() { }) } - Dimensions.addEventListener('change', onChange) + const subscription = Dimensions.addEventListener('change', onChange) return () => { - Dimensions.removeEventListener('change', onChange) + // @ts-expect-error - React Native >= 0.65 + if (typeof subscription?.remove === 'function') { + // @ts-expect-error - need update @types/react-native@0.65.x + subscription.remove() + } else { + // React Native < 0.65 + Dimensions.removeEventListener('change', onChange) + } } }, []) diff --git a/src/useDimensions.ts b/src/useDimensions.ts index f0a3ea41..8a2259aa 100644 --- a/src/useDimensions.ts +++ b/src/useDimensions.ts @@ -18,9 +18,18 @@ export function useDimensions() { } useEffect(() => { - Dimensions.addEventListener('change', onChange) + const subscription = Dimensions.addEventListener('change', onChange) - return () => Dimensions.removeEventListener('change', onChange) + return () => { + // @ts-expect-error - React Native >= 0.65 + if (typeof subscription?.remove === 'function') { + // @ts-expect-error - need update @types/react-native@0.65.x + subscription.remove() + } else { + // React Native < 0.65 + Dimensions.removeEventListener('change', onChange) + } + } }, []) return dimensions