You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
After upgrading to react navigation 5, back button handle for exit route not working, in fact it's not even registering, so any routes defined in export const exitRoutes: string[] = ["welcome", "demo"] won't exit app, but trigger default bahavior
Expected result
App will exit if currently showing route in exitRoutes
Steps to reproduce
Install fresh ignite bowser
Change the exit routes, add "demo" route
Run app
Navigate to demo screen
Press hardware back
It will go back to welcome screen instead of exiting app
Current workaround
Using device event emiter, this will remove all hardwareBackPress handler from other libs too
import { useEffect, useRef, useState } from "react"
import { BackHandler, DeviceEventEmitter } from "react-native"
import { NavigationContainerRef } from "@react-navigation/native"
import getActiveRouteName from "./get-active-routename"
export function useBackButtonHandler(
ref: React.RefObject<NavigationContainerRef>,
canExit: (routeName: string) => boolean,
) {
const [backPressSubscriptions] = useState(new Set())
const canExitRef = useRef(canExit)
useEffect(() => {
canExitRef.current = canExit
}, [canExit])
useEffect(() => {
// We'll fire this when the back button is pressed on Android.
const handleBackPress = () => {
const navigation = ref.current
if (navigation == null) {
return false
}
// grab the current route
const routeName = getActiveRouteName(navigation.getRootState())
// are we allowed to exit?
if (canExitRef.current(routeName)) {
// let the system know we've not handled this event
return false
}
// we can't exit, so let's turn this into a back action
if (navigation.canGoBack()) {
navigation.goBack()
return true
}
return false
}
// Subscribe when we come to life
DeviceEventEmitter.removeAllListeners("hardwareBackPress")
DeviceEventEmitter.addListener("hardwareBackPress", () => {
let invokeDefault = true
const subscriptions = []
backPressSubscriptions.forEach(sub => subscriptions.push(sub))
for (let i = 0; i < subscriptions.reverse().length; i += 1) {
if (subscriptions[i]()) {
invokeDefault = false
break
}
}
if (invokeDefault) {
BackHandler.exitApp()
}
})
backPressSubscriptions.add(handleBackPress)
// Unsubscribe when we're done
return () => {
DeviceEventEmitter.removeAllListeners("hardwareBackPress")
backPressSubscriptions.clear()
}
}, [ref])
}
Using custom back handler by react navigation 5 https://reactnavigation.org/docs/custom-android-back-button-handling, put this in every screen you want to exit on back press, current issue, it won't register on the first time, so back will still go back to previous screen
What's going on?
After upgrading to react navigation 5, back button handle for exit route not working, in fact it's not even registering, so any routes defined in
export const exitRoutes: string[] = ["welcome", "demo"]
won't exit app, but trigger default bahaviorExpected result
App will exit if currently showing route in exitRoutes
Steps to reproduce
It will go back to welcome screen instead of exiting app
Current workaround
ignite doctor
results:The text was updated successfully, but these errors were encountered: