0

I'm trying to set some conditions on the React Navigation headers (for example, if the cart contains items and you want to go back to the home screen, display an alert, and empty cart). The cart is managed in Redux.

How do I get the "currentRouteName" constant to use it as a conditional in "DrawerStack" for example?

I try to use redux to store this value, but it crashes the app.

Also, I don't know why but also the tracking event for amplitude is crashing my app too.

Thanks for the help!

The structure of my screens goes as follows:

<NavigationContainer
            ref={navigationRef}
            onReady={() => {
                SplashScreen.hideAsync();
                routeNameRef.current = navigationRef.current.getCurrentRoute().name;
            }}
            onStateChange={() => {
                const previousRouteName = routeNameRef.current;
                const currentRouteName = navigationRef.current.getCurrentRoute().name;
                if (previousRouteName !== currentRouteName) {
                    routeNameRef.current = currentRouteName;
                    trackAmplitudeEvent("Route",
                         {
                             name: currentRouteName,
                             ...restaurantProfileId && {restaurantProfileId}
                         })
                }
            }}
        >
           

--------------

<Drawer.Navigator
            drawerContent={props => <CustomDrawer {...props} />}
            screenOptions={{
                drawerPosition: "right",
                drawerActiveTintColor: "#fff",
                drawerInactiveTintColor: "#3D3D3D",
                drawerActiveBackgroundColor: foodhiRed,
                drawerLabelStyle: { fontSize: 16, marginLeft: -20 }
            }}>
            <Drawer.Screen
                name="Home"
                component={HomeStack}
                options={{
                    drawerIcon: ({ color }) => <View><Ionicons name="home-outline" size={26} color={color} /></View>,
                    headerTitle: () => (
                        <Text
                            onPress={() => {
                                if (cartItems.length > 0) {
                                    Alert.alert(
                                        "Warenkorb wird geleert",
                                        "Wenn du zum Hauptmenü zurückkehrst, wird der Warenkorb zurückgesetzt",
                                        [
                                            {
                                                text: "Ja", onPress: () => {
                                                    dispatch(emptyCart());
                                                    navigation.popToTop();
                                                },
                                                style: "default"
                                            },
                                            {
                                                text: "Abbrechen",
                                                style: "cancel"
                                            }
                                        ]
                                    )
                                } else navigation.popToTop();
                            }}
                            style={{
                                fontSize: 24,
                                color: "#fff",
                                fontFamily: 'Rubik_600SemiBold',
                            }}>
                            Foodhi
                        </Text>
                    ),
                    headerTintColor: "#fff",
                    headerShadowVisible: false,
                    headerStyle: {
                        backgroundColor: foodhiGreen,
                    },
                    headerLeft: () => {
                        if (screenName !== "StoresIndex") {
                            return (
                                <Pressable onPress={() => emptyCartAlert()}>
                                    <Icon name="chevron-left" color="#fff" size={32} style={{ paddingLeft: 10 }} />
                                </Pressable>
                            )
                        }
                    },
                    headerRight: () => (
                        <TouchableOpacity style={{ marginRight: 20 }}
                            onPress={() => navigation.dispatch(DrawerActions.openDrawer())}>
                            <Icon name="menu" size={30} color="#fff" />
                        </TouchableOpacity>
                    ),
                }}
            />
           
        </Drawer.Navigator >
AmerllicA
  • 29,059
  • 15
  • 130
  • 154
Eric Lehmann
  • 329
  • 2
  • 13
  • Put your crash details and ping me down there. – AmerllicA Aug 19 '23 at 11:16
  • On Sentry I get NSRangeException RNSScreen *** -[__NSArrayM objectAtIndexedSubscript:]: index 2 beyond bounds [0 .. 1] Thread 9 name: Dispatch queue: com.facebook.react.AmplitudeReactNativeQueue Thread 9 Crashed: 0 libsystem_kernel.dylib 0x1d5c13674 __pthread_kill + 8 1 libsystem_pthread.dylib 0x1e63771ac pthread_kill + 268 2 libsystem_c.dylib 0x19f774c8c abort + 180 3 libc++abi.dylib 0x1e62b6b8c abort_message + 132 4 libc++abi.dylib 0x1e62a6a80 demangling_terminate_handler() .. – Eric Lehmann Aug 19 '23 at 14:35
  • Oh, and thanks for the corrections. – Eric Lehmann Aug 19 '23 at 14:40
  • Did you manage to take a look by any chance? – Eric Lehmann Aug 21 '23 at 22:17

0 Answers0