0

I'm using React Navigation 6.x's linking with Expo so that when a user clicks on a notification they are directed to the appropriate part of my application to interact with the new information. When my app is backgrounded (running in the background) and a user clicks on a notification they are redirected to the screen they need to be at, which works perfectly fine. However, when the app is killed and the user clicks on a notification, they are taken directly to the screen for which the url is provided and they cannot press back to navigate elsewhere in my application. I tried to resolve this by using the initialRouteName prop like is shown in the docs (link: https://reactnavigation.org/docs/configuring-links/#rendering-an-initial-route), but I cannot get it to work. For further clarification, when I mentioned above that I am able to get linking working it is in relation to the direct SettingsScreen, AddFriendScreen, and MessagingScreen links. What I cannot get working is the specific block of code inside the liking object that starts with HomeScreen. What I believe may be causing the issue is that I am trying to set my initialRoute as a screen within HomeScreen's Tabs.Navigator and then trying to route to a screen within my Stack.Navigator. However, the docs show that this is possible (link: https://reactnavigation.org/docs/configuring-links/#rendering-an-initial-route).

My code:

const linking = {
    prefixes: [prefix],
    config: {
        screens: {
            HomeScreen: {
                initialRouteName: "Chats",
                screens: {
                    AddFriendScreen: "addFriend",
                    CreateChatScreen: "createChatScreen",
                    Friends: "friends",
                    MessagingScreen: 'messagingScreen/:chatRoomId'
                }
            },
            SettingsScreen: "SettingsScreen",
            AddFriendScreen: "AddFriendScreen",
            MessagingScreen: 'MessagingScreen/:chatRoomId'
        },
    }
};

<NavigationContainer linking={linking} theme={MyTheme} >
    <Stack.Navigator>
        {!authState.value ? 
            <>
                <Stack.Screen name="LoginScreen" component={LoginScreen} />
                <Stack.Screen name="SignUpScreen" component={SignUpScreen} />
                <Stack.Screen name="ForgotPasswordScreen" component={ForgotPasswordScreen} />
            </>
        :
            <>
                <Stack.Screen name="HomeScreen" component={HomeScreen} />
                <Stack.Screen name="MessagingScreen" component={MessagingScreen} />
                <Stack.Screen name="SettingsScreen" component={SettingsScreen} />
                <Stack.Screen name="CreateChatScreen" component={CreateChatScreen} />
                <Stack.Screen name="AddFriendScreen" component={AddFriendScreen} />
            </>
        }
    </Stack.Navigator>
</NavigationContainer>

const HomeScreen = () => {
    return (
        <Tabs.Navigator>
            <Tabs.Screen name="Chats" component={ChatScreen} />
            <Tabs.Screen name="Friends" component={FriendsScreen} />
            <Tabs.Screen name="Profile" component={ProfileScreen} />
        </Tabs.Navigator>
    )
}
man517
  • 419
  • 5
  • 9

1 Answers1

0

Hmm, I would put any screen that I would like to access in multiple navigators at the root level so that they can both reach it.

For my own project, I have a root stack with a dedicated auth stack and an app/home stack (which is a tab nav like your HomeScreen). And outside that I have all my modal and root screens that I'd like other navigators to access (Because I really have two Home tab navigators the user can switch between.)

Idk if putting all of your screens listed after HomeScreen outside of that object will help your situation, but you can try that.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 17 '22 at 05:18