Mostav
Mostav

Reputation: 2612

React-Native Stack navigator go back to previous screen with same initialRouteName

I am developing a React Native app and am using react navigation's stacknavigator, and here is my scenario:

<AppStack.Navigator
    initialRouteName="HOME"
    screenOptions={stackStyle}>
    <AppStack.Screen
        name="HOME"
        component={A}
    />
    <AppStack.Screen
        name="B"
        component={B}
    />
....

Within B:

const {navigation} = this.props;
navigation.navigate('B', {new data for second B});

EDIT: Forgot to mention that first B trigger second B from a webview

Upvotes: 2

Views: 2978

Answers (1)

HichamELBSI
HichamELBSI

Reputation: 1719

In my opinion, you should deal with the state of your components to rerender. Anyway, if you just need to navigate to the same page and keep the go back, you can replace :

navigation.navigate('B', { ... })

by :

navigation.push('B', { ... })

When you use push, you create a new route so when you will go back from this new route, it will navigate to the previous one. navigate will try to go to an existing route so in your case: the route B already exists actually.

Upvotes: 2

Related Questions