-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from retyui/use-modern-event-emitter-api
[RN 0.65.x] Adapt unsubscription for new EventEmitter API
- Loading branch information
Showing
4 changed files
with
44 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/[email protected] | ||
subscription.remove() | ||
} else { | ||
// React Native < 0.65 | ||
AccessibilityInfo.removeEventListener(eventName, setIsEnabled) | ||
} | ||
} | ||
}, [eventName, initializerName]) | ||
|
||
return isEnabled | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/[email protected] | ||
subscription.remove() | ||
} else { | ||
// React Native < 0.65 | ||
AppState.removeEventListener('change', onChange) | ||
} | ||
} | ||
}, []) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/[email protected] | ||
subscription.remove() | ||
} else { | ||
// React Native < 0.65 | ||
Dimensions.removeEventListener('change', onChange) | ||
} | ||
} | ||
}, []) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/[email protected] | ||
subscription.remove() | ||
} else { | ||
// React Native < 0.65 | ||
Dimensions.removeEventListener('change', onChange) | ||
} | ||
} | ||
}, []) | ||
|
||
return dimensions | ||
|