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 Chat Input Crashes on Android #20058

Merged
merged 4 commits into from
Oct 2, 2019

Conversation

MarcoPolo
Copy link
Contributor

The input was crashing when you sent messages that had modified the
native selection props. It crashed when you cleared the text because it
would set the text to empty, and reused a cached selection that was out
of bounds of an empty string.

For example, say you type "hello @" and autocomplete to "hello @foo". We
do some text selection magic to insert the @foo and put your cursor in
the correct spot. The Selection start and end indicies are both 11. When
you submit to message, we clear the text. When we clear the text, RN
keeps reusing those selection indices (of 11) and Android complains the
selection is outside of the bounds of the text ("" has a length of 0,
but you want to move the cursor to position 11, outside the bounds of
0).

The workaround here is to set the selection at the same time as the
text.

The RN Issue is: facebook/react-native#25265

@buoyad since I think he has the most context here.
@keybase/react-hackers

The input was crashing when you sent messages that had modified the
native selection props. It crashed when you cleared the text because it
would set the text to empty, and reused a cached selection that was out
of bounds of an empty string.

For example, say you type "hello @" and autocomplete to "hello @foo". We
do some text selection magic to insert the @foo and put your cursor in
the correct spot. The Selection start and end indicies are both 11. When
you submit to message, we clear the text. When we clear the text, RN
keeps reusing those selection indices (of 11) and Android complains the
selection is outside of the bounds of the text ("" has a length of 0,
but you want to move the cursor to position 11, outside the bounds of
0).

The workaround here is to set the selection at the same time as the
text.

The RN Issue is: facebook/react-native#25265
Copy link
Contributor

@chrisnojima chrisnojima left a comment

Choose a reason for hiding this comment

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

Can we just do the same thing on ios ?

@MarcoPolo
Copy link
Contributor Author

I tried it and it seems fine on iOS too.

@@ -67,9 +67,13 @@ class PlainInput extends Component<InternalProps> {
}
const newTextInfo = fn(currentTextInfo)
checkTextInfo(newTextInfo)
this.setNativeProps({text: newTextInfo.text})
// If we're android, let's workaround this Issue: https://github.com/imnapo/react-native-cn-richtext-editor/issues/81
Copy link
Contributor

Choose a reason for hiding this comment

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

Update comment

this.setNativeProps({
selection: this._sanityCheckSelection(newTextInfo.selection, newTextInfo.text),
text: newTextInfo.text,
})
this._lastNativeText = newTextInfo.text
Copy link
Contributor

Choose a reason for hiding this comment

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

We should likely set this._lastNativeSelection here

@MarcoPolo MarcoPolo merged commit 26ad695 into master Oct 2, 2019
@MarcoPolo MarcoPolo deleted the marco/android-input-crash-workaround branch October 2, 2019 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants