Define some functions in app.js | index.js
. Means initial screen
setActiveScreen = (activeScreen) => {
const previousScreen = this.state.activeScreen
this.setState({activeScreen: activeScreen, previousScreen: previousScreen})
}
goBackScreen = () => {
this.setState({activeScreen: this.state.previousScreen});
}
And pass this functions to the navigation screens
<Stack.Screen >
{(props) => <ComponentScreen goBackScreen={this.goBackScreen} setActiveScreen={this.setActiveScreen} /> }
</Stack.Screen>
Then run the goBackScreen
before you use the navigation.goBack()
in screens
<TouchableOpacity onPress={() => {
this.props.goBackScreen()
this.props.navigation.goBack();
}}><Text>Click</Text> </TouchableOpacity>
Note
: now you can get the previous screen name in activeScreen. It's just an example for the possibility. And you can try your own idea
Or you can pass the previous screen in params
navigation.navigate("home", {params: {previousScreen: '{{pass the currentScreen name}}'
then the next screen you can get it by console.log(this.props.route.params)