Skip to content

Commit

Permalink
Merge pull request #268 from retyui/use-modern-event-emitter-api
Browse files Browse the repository at this point in the history
[RN 0.65.x] Adapt unsubscription for new EventEmitter API
  • Loading branch information
LinusU authored Oct 19, 2021
2 parents f383f0f + 53fd7b5 commit 70504da
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
17 changes: 15 additions & 2 deletions src/useAccessibilityInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/useAppState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}, [])

Expand Down
11 changes: 9 additions & 2 deletions src/useDeviceOrientation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}, [])

Expand Down
13 changes: 11 additions & 2 deletions src/useDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 70504da

Please sign in to comment.