Giter Site home page Giter Site logo

react-navigation-backhandler's Introduction

react-navigation-backhandler

Easily handle Android back button behavior with React-Navigation.

NOTE use version 1 of this package for react-navigation version 4 and lower

use version 2 of this package for react-navigation version 5 and later

Installation

Install with npm:

$ npm install react-navigation-backhandler

Install with yarn:

$ yarn add react-navigation-backhandler

Usage

The following snippet demonstrates the usage. Note that onBackButtonPressAndroid will only be called if SomeComponent is placed in a screen that is focused (the one user is directly interacting with).

Behind the scenes, the onBackButtonPressAndroid handler is registered before a screen is focused, and unregistered before going away from it, leaving you with a declarative interface to interact with. Internally, this package uses apis that are provided by react-navigation.

You may use useAndroidBackHandler or AndroidBackHandler component anywhere in your app's React tree, it does not need to be placed directly in the screen component.

There are two ways of using this library:

  1. As hook
  2. As component

Use as hook

import { useAndroidBackHandler } from "react-navigation-backhandler";

const SomeComponent = () => {
  const onBackPress = useCallback(() => {
    /*
     *   Returning `true` denotes that we have handled the event,
     *   and react-navigation's lister will not get called, thus not popping the screen.
     *
     *   Returning `false` will cause the event to bubble up and react-navigation's listener will pop the screen.
     * */

    if (youWantToHandleTheBackButtonPress) {
      // do something
      return true
    }

    return false
  }, [])

  useAndroidBackHandler(onBackPress)

  return <BodyOfYourScreen />
}

Use as component

Note: You can also use the component "inline" without children: <AndroidBackHandler onBackPress={this.onBackButtonPressAndroid} />

import { AndroidBackHandler } from "react-navigation-backhandler";

class SomeComponent extends React.Component {
  onBackButtonPressAndroid = () => {
    /*
     *   Returning `true` from `onBackButtonPressAndroid` denotes that we have handled the event,
     *   and react-navigation's lister will not get called, thus not popping the screen.
     *
     *   Returning `false` will cause the event to bubble up and react-navigation's listener will pop the screen.
     * */

    if (youWantToHandleTheBackButtonPress) {
      // do something
      return true;
    }

    return false;
  };

  render() {
    return (
      <AndroidBackHandler onBackPress={this.onBackButtonPressAndroid}>
        <BodyOfYourScreen />
      </AndroidBackHandler>
    );
  }
}

Warning

The package was only tested to be used in screens in stack navigator, other use cases may not work.

react-navigation-backhandler's People

Contributors

akatroj avatar andrejborstnik avatar jeanregisser avatar jpeer264 avatar londonoliver avatar mamousavi avatar murtraja avatar ohjeyong avatar vonovak avatar youngjuning avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-navigation-backhandler's Issues

Support for Web

Hello,

While compiling for web (using react-native-web), I receive this error:

ERROR in ./node_modules/react-navigation-backhandler/index.js
Module not found: Error: Can't resolve './BackHandler' in '/home/wishbook/MMR/code/reactapp/node_modules/react-navigation-backhandler'
 @ ./node_modules/react-navigation-backhandler/index.js 1:204-228
 @ ./app/screens/home/HomeScreen.js
 @ ./app/config/routes.js
 @ ./app/index.js
 @ ./index.web.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./node_modules/regenerator-runtime/runtime.js ./index.web.js

This issue is easily fixed by having a BackHandler.web.js file whose contents are same as that of Backhandler.ios.js

Would you like to me submit a PR, so that this library is react-native-web compatible?

Support newer react-navigation

On the peer dependencies only version 1 and 2 are supported, thus I'm having this warning:
warning " > [email protected]" has incorrect peer dependency "react-navigation@^1.0.0 || ^2.0.0".

Could you please update the peer dependency on your package.json?

Version 1.3.2 mysteriously ignores previously nested <AndroidBackHandler /> no matter what

Admittedly our use case is not exclusively within a StackNavigator but none of our code has changed.

Unfortunately upon revisiting the project the nested AndroidBackHandlers are totally ignored, not run at all, and only the highest level onBackPress is captured and run.

Returning false, true at the highest level has no effect, and any other actions in nested handlers don't work to preempt this because the functions simply aren't run. Any thoughts would be greatly appreciated because we're otherwise looking at a huge amount of unexpected work and rethinking of our application.

I've examined my yarn.lock to try to find differences in version of this package or react-navigation packages, but I don't see anything wrong there either.

BackHandler listener not removed

When calling navigation.popToTop() in a StackNavigator, willUnmount is called before willBlur, and the BackHandler listener is not removed.

TabNavigator Android back button

Hi there! I'm using your package with a screen inside a TabNavigator and it looks like my back button callback is only being called from the second time the tab is changed. What I mean is, when I arrive at the screen the callback is not called when I press back on the device. Only when I navigate away from the screen and then come back does the back button behave as I expect.

If I press back button very fast, onBackPress is ignored.

Let's say app has StackNavigator and there are two stacks S1, S2.
Let's say S1 is my app's base screen. I wrapped AndroidBackHandler on S1 component not to close app when user press back button.
In S1, if I press back button, then onBackPress function works great!
After I push S2, if I press back button, then S2 disappear. If I press back button on S1, then onBackPress function works.
However, after I push S2 screen, then if I press back button twice very fast, then app closed without calling onBackPress function.

I think If I press back button twice very fast, the second back event is triggered before AndroidBackHandler attach didFocus event on S1.

Not working in the production mode.

I am using and backhander is not working in the production app. As it is working fine in the debug mode. Also I am using in other screens and working fine. But not working in a single screen. I am not getting why.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.