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] [BackAndroid] New listeners registered while back event is handled also receive the current event #5781

Closed
fabianeichinger opened this issue Feb 5, 2016 · 2 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@fabianeichinger
Copy link
Contributor

I just faced for me unexpected behavior when (indirectly) registering a listener to BackAndroid while an event is currently handled.

In my project I use a Root component type, which calls a function provided to it via the onBackPress prop. It looks something like this:

export default class Root extends React.Component {

  _handleBackButton = () => {
    if (this.props.onBackPress) {
      this.props.onBackPress();
      return true;
    }
    return false;
  };

  componentDidMount() {
    BackAndroid.addEventListener('hardwareBackPress', this._handleBackButton);
  }

  componentWillUnmount() {
    BackAndroid.removeEventListener('hardwareBackPress', this._handleBackButton);
  }

  render() {
    return (
      …
  }
}

Now if the back button is pressed and, as part of the onBackPress function, the current Root element is replaced by a new one, the new Root element's componentDidMount is executed before onBackPress returns, adding the new event listener to BackAndroid.
Problem is: BackAndroid will add the new listener to the Set of listeners it loops over and eventually call the newly added listener for the same event it was created by.

I think the better behavior would be not to modify the Set of listeners while looping over them to dispatch an event. In case the Set needs to be modified create a copy, apply the modification there and continue to use the original Set to complete handling of the current event.

@facebook-github-bot
Copy link
Contributor

Hey feichngr, thanks for reporting this issue!

React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.

  • If you don't know how to do something or something is not working as you expect but not sure it's a bug, please ask on StackOverflow with the tag react-native or for more real time interactions, ask on Discord in the #react-native channel.
  • If this is a feature request or a bug that you would like to be fixed, please report it on Product Pains. It has a ranking feature that lets us focus on the most important issues the community is experiencing.
  • We welcome clear issues and PRs that are ready for in-depth discussion. Please provide screenshots where appropriate and always mention the version of React Native you're using. Thank you for your contributions!

ghost pushed a commit that referenced this issue Feb 8, 2016
Summary:
…while an event is dispatched

While it is guarded, a copy of the Set is created before listeners are added or removed. The event dispatch loop continues with the old Set of listeners.

This PR modifies `BackAndroid` to match the proposal at the end of #5781.
Closes #5783

Reviewed By: svcscm

Differential Revision: D2911282

Pulled By: foghina

fb-gh-sync-id: 34964ec3414af85eb9574bbcef081238fc67ffaf
pglotov pushed a commit to pglotov/react-native that referenced this issue Mar 15, 2016
Summary:
…while an event is dispatched

While it is guarded, a copy of the Set is created before listeners are added or removed. The event dispatch loop continues with the old Set of listeners.

This PR modifies `BackAndroid` to match the proposal at the end of facebook#5781.
Closes facebook#5783

Reviewed By: svcscm

Differential Revision: D2911282

Pulled By: foghina

fb-gh-sync-id: 34964ec3414af85eb9574bbcef081238fc67ffaf
@mkonicek
Copy link
Contributor

Hi there! This issue is being closed because it has been inactive for a while.

But don't worry, it will live on with ProductPains! Check out its new home: https://productpains.com/post/react-native/android-backandroid-new-listeners-registered-while-back-event-is-handled-also-receive-the-current-event

ProductPains helps the community prioritize the most important issues thanks to its voting feature.
It is easy to use - just login with GitHub. GitHub issues have voting too, nevertheless
Product Pains has been very useful in highlighting the top bugs and feature requests:
https://productpains.com/product/react-native?tab=top

Also, if this issue is a bug, please consider sending a pull request with a fix.
We're a small team and rely on the community for bug fixes of issues that don't affect fb apps.

@facebook facebook locked as resolved and limited conversation to collaborators Jul 20, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

5 participants