• Stars
    star
    198
  • Rank 196,898 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easily handle Android back button behavior with React-Navigation.

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

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 = () => {
  useAndroidBackHandler(() => {
    /*
     *   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;
  });

  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.

More Repositories

1

react-navigation-header-buttons

Easily render header buttons for react-navigation
TypeScript
808
star
2

react-navigation-props-mapper

Forwards react-navigation params to your screen component's props directly
TypeScript
397
star
3

react-native-add-calendar-event

Create, view or edit events in react native using the standard iOS / Android dialogs
Objective-C
321
star
4

react-native-simple-toast

Cross-platform Toast experience for React Native
Objective-C++
286
star
5

react-native-theme-control

Natively control react native application theme at runtime and persist it for the next app start.
TypeScript
110
star
6

react-native-bottom-toolbar

UI Bar in javascript for react native
JavaScript
47
star
7

react-navigation-mst-demo

demo of react-navigation with mobx state tree
JavaScript
37
star
8

react-native-round-checkbox

iOS-styled round checkbox for RN
JavaScript
30
star
9

react-native-platforms

React Native Platform Shortcuts
TypeScript
22
star
10

navigation-experimental-fork

NavigationExperimental from RN 0.43.3. Useful when you still depend on it in RN >= 0.44.
JavaScript
7
star
11

react-native-flabel-textfield

Floating label text input for RN
JavaScript
5
star
12

react-to-imperative

Convert React (Native) components for imperative use.
TypeScript
5
star
13

react-native-radio-button-android

native android radio button for react native
Java
4
star
14

text_locator

ROS package for detecting and recognizing text in images
C++
3
star
15

react-native-keyboard-utils

Simple components for common use cases with keyboard
JavaScript
2
star
16

react-native-document-picker

replacement for accidentally deleted fork - to be deleted. Please use https://www.npmjs.com/package/react-native-document-picker
C#
2
star
17

phone8uip

bachelor thesis text
TeX
1
star
18

vonovak

1
star