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

[Android] [Fixed] - Fix InputAccessoryView crash on Android #33803

Closed
wants to merge 6 commits into from

Conversation

hduprat
Copy link
Contributor

@hduprat hduprat commented May 10, 2022

Summary

InputAccessoryView works fine on iOS, but crashes on Android - you can see that by using an Android device on the Expo Snack from the official doc.
It forces the developer not to render the component on Android, which is usually good, but other components have implemented other, safer ways to deal with incompatibility issues.

I am of course open to discussion about this change, as well as other implementation ideas.

Changelog

[Android] [Fixed] - Fix InputAccessoryView crash on Android

Test Plan

yarn test gives out the following output:
image

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels May 10, 2022
@react-native-bot react-native-bot added Bug Platform: Android Android applications. labels May 10, 2022
@analysis-bot
Copy link

analysis-bot commented May 10, 2022

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,824,160 -7
android hermes armeabi-v7a 7,211,020 -13
android hermes x86 8,134,713 -7
android hermes x86_64 8,114,961 +0
android jsc arm64-v8a 9,690,753 +17
android jsc armeabi-v7a 8,446,850 +17
android jsc x86 9,642,144 +17
android jsc x86_64 10,239,313 +19

Base commit: f97c6a5
Branch: main

@analysis-bot
Copy link

analysis-bot commented May 10, 2022

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 8dded42
Branch: main

@cortinico
Copy link
Contributor

cortinico commented May 12, 2022

Thanks for sending this over @hduprat

I am of course open to discussion about this change, as well as other implementation ideas.

I'd say we should rather rename the file to be InputAccessoryView.ios.js and create InputAccessoryView.android.js by just exporting Unimplemented view (as we do for other components like https://github.com/facebook/react-native/tree/main/Libraries/Components/DrawerAndroid)

@hduprat
Copy link
Contributor Author

hduprat commented May 19, 2022

I'd say we should rather rename the file to be InputAccessoryView.ios.js and create InputAccessoryView.android.js by just exporting Unimplemented view (as we do for other components like https://github.com/facebook/react-native/tree/main/Libraries/Components/DrawerAndroid)

Thank you for your suggestion @cortinico , I implemented it.
However there is a failing test and I do not know why.

@cortinico
Copy link
Contributor

However there is a failing test and I do not know why.

Please rebase and it should be green

@hduprat hduprat force-pushed the fix/InputAccessoryView branch 2 times, most recently from 8634189 to 5309566 Compare May 20, 2022 07:19
@hduprat
Copy link
Contributor Author

hduprat commented May 20, 2022

Please rebase and it should be green

There's still an error, but I see the CI fails on the main branch too.

@hduprat
Copy link
Contributor Author

hduprat commented May 30, 2022

@cortinico I have just rebased and all tests have finally passed. Thank you for fixing the Android tests 🙏

@facebook-github-bot
Copy link
Contributor

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Comment on lines 91 to 93
if (Platform.OS !== 'ios') {
console.warn('<InputAccessoryView> is only supported on iOS.');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

From an internal discussion:

Since InputAccessoryView is kind of like a "portal" (i.e. components render output is rendered inside a special native container), I think it would be better to make this return null.

Also, we wouldn't require call sites to use Flow suppressions or hacks.
If we wanted to do this a bit more efficiently (with dead code elimination), we could do something like:

function InputAccessoryView(props: Props): React.Node {
  if (Platform.OS === 'ios') {
    // Lines 95 - 106.
  } else {
    console.warn('…');
    return null;
  }
}
module.exports = InputAccessoryView;

cc @yungsters

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 updated my PR according to the comment.

However, it does not cover the case where InputAccessoryView wraps a TextInput - it would display nothing instead of the TextInput itself. Wouldn't it be considered as a bug later? Or do we want to be clear with developers that InputAccessoryView should never be used on Android?

Either way, it is better than a crash.

hduprat added 6 commits June 21, 2022 14:57
It prevents a crash when using InputAccessoryView on Android without specifying `Platform.OS === 'ios'`
As done on DrawerLayoutAndroid
This reverts commit db3d40f.
@hduprat hduprat changed the title [Android] [Fixed] - Display InputAccessoryView as UnimplementedView [Android] [Fixed] - Fix InputAccessoryView crash on Android Jun 21, 2022
@hduprat hduprat requested a review from cortinico June 27, 2022 07:52
@facebook-github-bot
Copy link
Contributor

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @hduprat in afa5df1.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Platform: Android Android applications. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants