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

KeyboardGestureArea + nested native-stack causes a crash #274

Closed
kirillzyusko opened this issue Nov 16, 2023 · 1 comment · Fixed by #275
Closed

KeyboardGestureArea + nested native-stack causes a crash #274

kirillzyusko opened this issue Nov 16, 2023 · 1 comment · Fixed by #275
Assignees
Labels
🤖 android Android specific 🐛 bug Something isn't working 🎯 crash Library triggers a crash of the app 👆 interactive keyboard Anything related to interactive keyboard dismissing

Comments

@kirillzyusko
Copy link
Owner

Describe the bug

A combination of KeyboardGestureArea and nested native-stack causes a crash.

Code snippet

  • replace createStackNavigator -> createNativeStackNavigator in example app
  • add this code in InteractiveKeyboard
<Button title='Go to' onPress={() => {navigation.navigate(ScreenNames.ANIMATED_EXAMPLE)}} />
  • remove implementation of KeyboardAnimation component (return null).

Repo for reproducing

Example app with modifications above.

To Reproduce
Steps to reproduce the behavior:

  1. Go to InteractiveKeyboard (android)
  2. Click on "Go to" button
  3. Go back
  4. Try to scroll a screen

Expected behavior

No crash. InteractiveKeyboard should work as before.

Screenshots

image

Smartphone (please complete the following information):

  • Desktop OS: [e.g. Windows 10, MacOS 10.15.5]
  • Device: Pixel 7 Pro
  • OS: Android 14
  • RN version: 0.72.5
  • RN architecture: old
  • JS engine: Hermes
  • Library version: 1.9.2

Additional context

Need to understand why isLaidOut is false.

@kirillzyusko kirillzyusko added 🐛 bug Something isn't working 🤖 android Android specific 👆 interactive keyboard Anything related to interactive keyboard dismissing 🎯 crash Library triggers a crash of the app labels Nov 16, 2023
@kirillzyusko kirillzyusko self-assigned this Nov 16, 2023
kirillzyusko added a commit that referenced this issue Nov 16, 2023
## 📜 Description

Removed `isLaidOut` check from `copyBoundsInWindow` method.

## 💡 Motivation and Context

When we are using `native-stack` then on initial mount the view property
`isLaidOut` will be `true`. When we open a new screen and return back to
the previous `isLaidOut` will be `false`.

I've tried to call `invalidate`/`requestLayout`/`forceLayout` methods in
`onAttachedToWindow` method, but it doesn't seem to have any effect at
all.

The I decided to compare how `copyBoundsInWindow` works if `isLaidOut`
check is removed. And turned out there is no difference between two
executions:

|Screen opened first time (isLaidOut check present)|Returned to the
screen (isLaidOut is missing to go to if-statement)|

|---------------------------------------------------|------------------------------------------------------------------|
|<img width="709" alt="Screenshot 2023-11-16 at 17 31 24"
src="https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/8f79bfae-3ed8-4e2c-abb6-a8d6a07eab28">|<img
width="696" alt="Screenshot 2023-11-16 at 17 52 55"
src="https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/4523be4d-36e7-4733-a71f-de7ff0dc5bd1">|

So taking this information into consideration I've decided to remove
that check.

Also for the sake of safety and avoidance of unexpected crashes I
decided to remove `throw Exception` construction and replace it with a
simple logger (since I'm not handling exceptions in my code it's better
to log it).

Closes
#274
#203

## 📢 Changelog

### Android
- removed `isLaidOut` check from `copyBoundsInWindow` method;
- replaced throwing error to logger.

## 🤔 How Has This Been Tested?

Tested on Pixel 7 Pro (android 14).

## 📸 Screenshots (if appropriate):


https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/ef8661da-8a78-4d4a-9362-668365736fed

## 📝 Checklist

- [x] CI successfully passed
@kirillzyusko
Copy link
Owner Author

Patch with changes for testing purpose:

diff --git a/example/src/navigation/ExamplesStack/index.tsx b/example/src/navigation/ExamplesStack/index.tsx
index 95d2e456..b3b7dde4 100644
--- a/example/src/navigation/ExamplesStack/index.tsx
+++ b/example/src/navigation/ExamplesStack/index.tsx
@@ -17,6 +17,7 @@ import KeyboardAvoidingViewExample from '../../screens/Examples/KeyboardAvoiding
 import ReanimatedChatFlatlist from '../../screens/Examples/ReanimatedChatFlatlist';
 import EnabledDisabled from '../../screens/Examples/EnabledDisabled';
 import AwareScrollViewStickyFooter from '../../screens/Examples/AwareScrollViewStickyFooter';
+import { createNativeStackNavigator } from '@react-navigation/native-stack';
 
 export type ExamplesStackParamList = {
   [ScreenNames.ANIMATED_EXAMPLE]: undefined;
@@ -34,7 +35,7 @@ export type ExamplesStackParamList = {
   [ScreenNames.ENABLED_DISABLED]: undefined;
 };
 
-const Stack = createStackNavigator<ExamplesStackParamList>();
+const Stack = createNativeStackNavigator<ExamplesStackParamList>();
 
 const options = {
   [ScreenNames.ANIMATED_EXAMPLE]: {
diff --git a/example/src/navigation/RootStack/index.tsx b/example/src/navigation/RootStack/index.tsx
index 353a1491..cee87187 100644
--- a/example/src/navigation/RootStack/index.tsx
+++ b/example/src/navigation/RootStack/index.tsx
@@ -4,13 +4,14 @@ import { createStackNavigator } from '@react-navigation/stack';
 import { ScreenNames } from '../../constants/screenNames';
 import ExamplesStack from '../ExamplesStack';
 import ExampleMain from '../../screens/Examples/Main';
+import { createNativeStackNavigator } from '@react-navigation/native-stack';
 
 export type RootStackParamList = {
   [ScreenNames.EXAMPLES]: undefined;
   [ScreenNames.EXAMPLES_STACK]: {};
 };
 
-const Stack = createStackNavigator<RootStackParamList>();
+const Stack = createNativeStackNavigator<RootStackParamList>();
 
 const options = {
   [ScreenNames.EXAMPLES_STACK]: { headerShown: false },
diff --git a/example/src/screens/Examples/InteractiveKeyboard/index.tsx b/example/src/screens/Examples/InteractiveKeyboard/index.tsx
index 3332cf43..958a5056 100644
--- a/example/src/screens/Examples/InteractiveKeyboard/index.tsx
+++ b/example/src/screens/Examples/InteractiveKeyboard/index.tsx
@@ -1,8 +1,9 @@
 import { StackScreenProps } from '@react-navigation/stack';
 import React, { useEffect, useState } from 'react';
-import { Text, TextInput, View } from 'react-native';
+import { Button, Text, TextInput, View } from 'react-native';
 import {
   KeyboardGestureArea,
+  useGenericKeyboardHandler,
   useKeyboardHandler,
 } from 'react-native-keyboard-controller';
 import Reanimated, {
@@ -14,6 +15,7 @@ import Message from '../../../components/Message';
 import { history } from '../../../components/Message/data';
 import { ExamplesStackParamList } from '../../../navigation/ExamplesStack';
 import styles from './styles';
+import { ScreenNames } from '../../../constants/screenNames';
 
 const AnimatedTextInput = Reanimated.createAnimatedComponent(TextInput);
 
@@ -88,6 +90,7 @@ function InteractiveKeyboard({ navigation }: Props) {
         interpolator={interpolator}
         showOnSwipeUp
       >
+        <Button title='Go to' onPress={() => {navigation.navigate(ScreenNames.ANIMATED_EXAMPLE)}} />
         <Reanimated.ScrollView
           showsVerticalScrollIndicator={false}
           style={scrollViewStyle}
diff --git a/example/src/screens/Examples/KeyboardAnimation/index.tsx b/example/src/screens/Examples/KeyboardAnimation/index.tsx
index 7da048df..7da7a452 100644
--- a/example/src/screens/Examples/KeyboardAnimation/index.tsx
+++ b/example/src/screens/Examples/KeyboardAnimation/index.tsx
@@ -3,5 +3,5 @@ import React from 'react';
 import KeyboardAnimationTemplate from '../../../components/KeyboardAnimation';
 
 export default function KeyboardAnimation() {
-  return <KeyboardAnimationTemplate />;
+  return null;
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 android Android specific 🐛 bug Something isn't working 🎯 crash Library triggers a crash of the app 👆 interactive keyboard Anything related to interactive keyboard dismissing
Projects
None yet
1 participant