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

[Fix][useKeyboard] Adapt unsubscription for new EventEmitter API #279

Merged
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
13 changes: 1 addition & 12 deletions src/useKeyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,7 @@ export function useKeyboard() {
]

return () => {
if (Keyboard.removeListener) {
Copy link
Contributor Author

@retyui retyui Nov 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check work until the RN team decided to restore this method: facebook/react-native@035718b

Restore deprecated event listener removal methods in order to minimize breaking changes for the next release. The methods will work, but they will not report a warning via console.error.

Copy link

@wildseansy wildseansy Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support versions <= 0.64

if (subscriptions[0]?.remove) {
  subscriptions.forEach((subscription) => subscription.remove())
} else {
    Keyboard.removeListener('keyboardWillShow', handleKeyboardWillShow)
    Keyboard.removeListener('keyboardDidShow', handleKeyboardDidShow)
    Keyboard.removeListener('keyboardWillHide', handleKeyboardWillHide)
    Keyboard.removeListener('keyboardDidHide', handleKeyboardDidHide)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked 0.60.x , 0.61.x, 0.62.x RN versions

and in these versions addEventListner returned subscription with remove method

// React Native < 0.65
Keyboard.removeListener('keyboardWillShow', handleKeyboardWillShow)
Keyboard.removeListener('keyboardDidShow', handleKeyboardDidShow)
Keyboard.removeListener('keyboardWillHide', handleKeyboardWillHide)
Keyboard.removeListener('keyboardDidHide', handleKeyboardDidHide)
} else {
// React Native >= 0.65
for (const subscription of subscriptions) {
subscription.remove()
}
}
subscriptions.forEach((subscription) => subscription.remove())
}
}, [])
return {
Expand Down