react-navigation / react-navigation/react-navigation.github.io

Update [Custom Android back button behavior] example

Open
#460 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
324
Forks
2k
PR merge metrics
No merged PRs in 30d

Description

Hi! I am currently refactoring code to using hooks. And I found refactoring the custom back button behaviour confusing.

I tried to adapt the example you have here to the code I refactored

import React from "react";
import { BackHandler } from "react-native";

const ScreenWithCustomBackBehavior = ({ navigation }) => {

const onBackButtonPressAndroid = useCallback(() => {
    if (isSelectionModeEnabled()) {
      disableSelectionMode();
      return true;
    } else {
      return false;
    }
  }, [isSelectionModeEnabled, disableSelectionMode]);

  useEffect(() => {
    const _didFocusSubscription = navigation.addListener('didFocus', () => {
      BackHandler.addEventListener('hardwareBackPress', onBackButtonPressAndroid);
    });
    const _willBlurSubscription = navigation.addListener('willBlur', () => {
      BackHandler.removeEventListener('hardwareBackPress', onBackButtonPressAndroid);
    });

    return () => {
      if (_didFocusSubscription) {
        _didFocusSubscription.remove();
      }
      if (_willBlurSubscription) {
        _willBlurSubscription.remove();
      }
    };
  }, [navigation, onBackButtonPressAndroid]);
}

  return (
    // ...
  )

Do you think it's a proper way to do it? If yes, should I submit a PR?

Thank you

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the linked Custom Android back button behavior example and the proposed hooks-based snippet. Decide whether the example should be updated to this approach; done means the documentation example reflects the agreed behavior and is clear for readers.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, react-native
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.