question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Library not Compatible with React Native 0.65.1 - Keyboard.removeListener is not a function

See original GitHub issue

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch react-native-gifted-chat@0.16.3 for the project I’m working on.

Solving this issue: https://github.com/FaridSafi/react-native-gifted-chat/issues/2090 and https://github.com/FaridSafi/react-native-gifted-chat/issues/2094

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-gifted-chat/lib/MessageContainer.js b/node_modules/react-native-gifted-chat/lib/MessageContainer.js
index 193772a..637fdc9 100644
--- a/node_modules/react-native-gifted-chat/lib/MessageContainer.js
+++ b/node_modules/react-native-gifted-chat/lib/MessageContainer.js
@@ -63,10 +63,10 @@ export default class MessageContainer extends React.PureComponent {
         };
         this.detachKeyboardListeners = () => {
             const { invertibleScrollViewProps: invertibleProps } = this.props;
-            Keyboard.removeListener('keyboardWillShow', invertibleProps.onKeyboardWillShow);
-            Keyboard.removeListener('keyboardDidShow', invertibleProps.onKeyboardDidShow);
-            Keyboard.removeListener('keyboardWillHide', invertibleProps.onKeyboardWillHide);
-            Keyboard.removeListener('keyboardDidHide', invertibleProps.onKeyboardDidHide);
+            Keyboard.addListener('keyboardWillShow', invertibleProps.onKeyboardWillShow).remove();
+            Keyboard.addListener('keyboardDidShow', invertibleProps.onKeyboardDidShow).remove();
+            Keyboard.addListener('keyboardWillHide', invertibleProps.onKeyboardWillHide).remove();
+            Keyboard.addListener('keyboardDidHide', invertibleProps.onKeyboardDidHide).remove();
         };
         this.renderTypingIndicator = () => {
             if (Platform.OS === 'web') {

This issue body was partially generated by patch-package.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

5reactions
Johan-dutoitcommented, Oct 2, 2021

That looks like it’ll cause memory leaks. As the events are being added in the function above. So effectively the initial ones are never removed.

Here’s the patch I went with

diff --git a/node_modules/react-native-gifted-chat/lib/MessageContainer.js b/node_modules/react-native-gifted-chat/lib/MessageContainer.js
index 193772a..c639b01 100644
--- a/node_modules/react-native-gifted-chat/lib/MessageContainer.js
+++ b/node_modules/react-native-gifted-chat/lib/MessageContainer.js
@@ -47,6 +47,10 @@ const styles = StyleSheet.create({
     },
 });
 export default class MessageContainer extends React.PureComponent {
+    keyboardWillShowSubscription = null;
+    keyboardDidShowSubscription = null;
+    keyboardWillHideSubscription = null;
+    keyboardDidHideSubscription = null;
     constructor() {
         super(...arguments);
         this.state = {
@@ -55,18 +59,18 @@ export default class MessageContainer extends React.PureComponent {
         this.attachKeyboardListeners = () => {
             const { invertibleScrollViewProps: invertibleProps } = this.props;
             if (invertibleProps) {
-                Keyboard.addListener('keyboardWillShow', invertibleProps.onKeyboardWillShow);
-                Keyboard.addListener('keyboardDidShow', invertibleProps.onKeyboardDidShow);
-                Keyboard.addListener('keyboardWillHide', invertibleProps.onKeyboardWillHide);
-                Keyboard.addListener('keyboardDidHide', invertibleProps.onKeyboardDidHide);
+                this.keyboardWillShowSubscription = Keyboard.addListener('keyboardWillShow', invertibleProps.onKeyboardWillShow);
+                this.keyboardDidShowSubscription = Keyboard.addListener('keyboardDidShow', invertibleProps.onKeyboardDidShow);
+                this.keyboardWillHideSubscription = Keyboard.addListener('keyboardWillHide', invertibleProps.onKeyboardWillHide);
+                this.keyboardDidHideSubscription = Keyboard.addListener('keyboardDidHide', invertibleProps.onKeyboardDidHide);
             }
         };
         this.detachKeyboardListeners = () => {
             const { invertibleScrollViewProps: invertibleProps } = this.props;
-            Keyboard.removeListener('keyboardWillShow', invertibleProps.onKeyboardWillShow);
-            Keyboard.removeListener('keyboardDidShow', invertibleProps.onKeyboardDidShow);
-            Keyboard.removeListener('keyboardWillHide', invertibleProps.onKeyboardWillHide);
-            Keyboard.removeListener('keyboardDidHide', invertibleProps.onKeyboardDidHide);
+            this.keyboardWillShowSubscription?.remove()
+            this.keyboardDidShowSubscription?.remove()
+            this.keyboardWillHideSubscription?.remove()
+            this.keyboardDidHideSubscription?.remove()
         };
         this.renderTypingIndicator = () => {
             if (Platform.OS === 'web') {

0reactions
stale[bot]commented, Apr 17, 2022

Sorry, but this issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. BTW Thank you for your contributions 😀 !!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

react native - _reactNative.Keyboard.removeListener is not ...
Now I started to get this exception while trying to navigate between screens: _reactNative.Keyboard.removeListener is not a function. enter ...
Read more >
Keyboard
The Keyboard module allows you to listen for native events and react to them, as well as make changes to the keyboard, like...
Read more >
react-native-gifted-messenger
react -native-gifted-chat library is not compatible with React Native 0.65.1 ... Upgrade react native project to 0.65.1 to reproduce the following error:.
Read more >
react-native-cli | Yarn - Package Manager
React Native is distributed as two npm packages, react-native-cli and react-native . ... Fixed regression: @jest/create-cache-key-function dependency was ...
Read more >
[Solved]-_reactNative.Keyboard.removeListener is not a ...
Coding example for the question _reactNative.Keyboard.removeListener is not a function-React Native.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found