Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RN 0.65.x] Adapt unsubscription for new EventEmitter API #268

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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