Marvelous Ikechi
Marvelous Ikechi

Reputation: 2043

Navigate to component screen in parent stack from child stack

I'm trying to navigate to a screen on my Stack Navigation from my topTab navigation but can't find a way around it. I tried using navigation.getParent() but it throws this error TypeError: undefined is not an object (evaluating 'navigation.getParent().navigate')

Here is my code to properly explain:

        <Stack.Navigator
            screenOptions={{
                headerShown: false,
            }}
        >
            <Stack.Screen name="interests" component={Interests} />
            <Stack.Screen name="importSubscriptions" component={ImportSubscriptions} />
            <Stack.Screen name="subscriptions" component={Subscriptions} />
            <Stack.Screen name="bottomTab" component={MainBottomTab} />
            <Stack.Screen name="podcastItem" component={PodcastItemCard}/>
            <Stack.Screen name="player" component={Player} />
            <Stack.Screen name="tabNavigator" component={UsersInterestTab} />

        </Stack.Navigator>
 <Tab.Navigator
                initialRouteName="Feed"
                initialLayout={{ width: Dimensions.get('window').width }}
                screenOptions={{
                    tabBarActiveTintColor: Colors.white,
                    tabBarInactiveTintColor: Colors.gray_2,
                    activeBackgroundColor: Colors.blue_primary,
                    tabBarScrollEnabled: true,
                    headerShown: false,
                    tabBarIndicatorStyle: {
                        width: 0,
                        height: 0,
                        elevation: 0,

                    },
                    tabBarItemStyle: { alignItems: 'center', },
                    tabBarLabelStyle: { margin: 0, fontSize: fontSize.xsmall, fontFamily: fontFamily.EuclidCircularARegular, color: Colors.gray_2 },
                    tabBarStyle: {
                        backgroundColor: Colors.white,
                        padding: 0,
                        elevation: 0,   // for Android
                        shadowOffset: {
                            width: 0,
                            height: 0 // for iOS
                        },
                        width: '100%'

                    }
                }}
            >
                <Tab.Screen
                    name={'For you'}
                    component={DiscoverContent}
                    initialParams={{ id: 177 }}
                    options={{
                        tabBarLabel: ({ color, focused }) => (
                            <Text style={[styles.label, { color: color, backgroundColor: focused ? Colors.blue_primary : Colors.white }]}>For you</Text>
                        ),
                    }}
                />
                
                {
                    usersInterests.map((item) => (
                        <Tab.Screen
                            key={item.id}
                            name={item.name}
                            component={DiscoverContent}
                            initialParams={{ id: item.id }}
                            options={{
                                tabBarLabel: ({ color, focused }) => (
                                    <Text style={[styles.label, { color: color, backgroundColor: focused ? Colors.blue_primary : Colors.white }]}>{item.name}</Text>
                                ),
                            }}
                        />
                    ))
                }
            </Tab.Navigator>

I'm trying to access the player component from the tabNavigator screen. How can I go about that ?

Upvotes: 2

Views: 1828

Answers (3)

Sunil kumar
Sunil kumar

Reputation: 1

you can use useNavigation()

ref:-https://reactnavigation.org/docs/use-navigation/

import { useNavigation } from '@react-navigation/native';

    useNavigation().goBack();
  

Upvotes: 0

Marvelous Ikechi
Marvelous Ikechi

Reputation: 2043

The issue was basically the structure of my navigation. I restructured the navigation without wrapping each Navigator in an individual NavigationaContainer (while passing the Independent props) and it worked.

Upvotes: -1

Muhammad Numan
Muhammad Numan

Reputation: 25413

can you try useNavigation hook? it is working on my side perfectly

Usage

import {useNavigation} from '@react-navigation/native';


const navigation = useNavigation();

navigation.navigate("interests");
//or
navigation.push("interests");

Upvotes: 2

Related Questions