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

Conversation

retyui
Copy link
Contributor

@retyui retyui commented Nov 11, 2021

Summary

Issue: #268 (comment)

// React Native 0.65.x +
typeof Keyboard.removeListener // function
Keyboard.removeListener(...) // print warning

Test Plan

What's required for testing (prerequisites)?

  • React Native >= 0.65

What are the steps to reproduce (after prerequisites)?

Compatibility

OS Implemented
iOS
Android
Web

Checklist

  • I have tested this on a device and a simulator
  • I added the documentation in README.md
  • I updated the typed files (TS and Flow)
  • I've created a snack to demonstrate the changes: LINK HERE

@@ -50,17 +50,17 @@ 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

@@ -50,17 +50,17 @@ export function useKeyboard() {
]

return () => {
if (Keyboard.removeListener) {
if (typeof subscriptions?.[0]?.remove === 'function') {
Copy link
Member

Choose a reason for hiding this comment

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

subscriptions[0] always exists here.

Suggested change
if (typeof subscriptions?.[0]?.remove === 'function') {
if (typeof subscriptions[0].remove === 'function') {

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 recheck it on latest versions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you right RN [0.59.x...0.6x.x] always return EmitterSubscription with remove method, so this if block don't make sense anymore

https://github.com/facebook/react-native/blob/c20070f10458d48d6ac1eaac49e681e932bfb9fd/Libraries/EventEmitter/NativeEventEmitter.js#L41-L50

Copy link
Member

@LinusU LinusU left a comment

Choose a reason for hiding this comment

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

Neat, just one small nit

@retyui retyui force-pushed the use-keyboard-fix-event-emitter-warn branch from 13355e0 to 37566c5 Compare November 12, 2021 11:48
@retyui retyui requested a review from LinusU November 12, 2021 11:51
Copy link
Member

@LinusU LinusU left a comment

Choose a reason for hiding this comment

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

Amazing! 🎉

@LinusU LinusU added patch Increment the patch version when merged release Create a release when this pr is merged labels Nov 12, 2021
@LinusU LinusU merged commit 6a75661 into react-native-community:master Nov 12, 2021
@pvinis
Copy link
Member

pvinis commented Nov 12, 2021

🚀 PR was released in v2.8.1 🚀

@pvinis pvinis added the released This issue/pull request has been released. label Nov 12, 2021
@wildseansy
Copy link

Just to be clear, v2.8.1 won't support <= v0.64 anymore, which uses removeListener - https://reactnative.dev/docs/0.64/keyboard

@wildseansy
Copy link

wildseansy commented Nov 13, 2021 via email

@retyui retyui deleted the use-keyboard-fix-event-emitter-warn branch November 14, 2021 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch Increment the patch version when merged release Create a release when this pr is merged released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants