Last active
October 10, 2019 22:58
-
-
Save shinchit/273309eba7bad4880fb654434a02ddd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { StackActions, NavigationActions } from 'react-navigation'; | |
import { | |
TouchableHighlight, | |
View, | |
Text, | |
} from 'react-native'; | |
class SomeScreen extends React.Component { | |
navigateTo(routes) { | |
const { navigation } = this.props; | |
let actions = lodash.reduce( | |
!lodash.isArray(routes) ? [routes] : lodash.reverse(routes), | |
(result, route) => { | |
let action = result; | |
if (!(action instanceof Object)) { | |
action = NavigationActions.navigate({ routeName: action }); | |
} | |
return NavigationActions.navigate({ | |
routeName: route, | |
action, | |
}); | |
}, | |
); | |
if (!lodash.isArray(routes)) { | |
actions = NavigationActions.navigate({ routeName: actions }); | |
} | |
const resetAction = StackActions.reset({ | |
index: 0, | |
actions: [ | |
actions, | |
], | |
}); | |
navigation.dispatch(resetAction); | |
} | |
render() { | |
return { | |
<TouchableHighlight onPress={() => { navigateTo(['ScreenD', 'TabScreenA', 'StackScreenA']); }}> | |
<View> | |
<Text>Move to ScreenD->TabScreenA->StackScreenA</Text> | |
</View> | |
</TouchableHighlight> | |
<TouchableHighlight onPress={() => { navigateTo(['ScreenC']); }}> | |
<View> | |
<Text>Move to ScreenC</Text> | |
</View> | |
</TouchableHighlight> | |
<TouchableHighlight onPress={() => { navigateTo('ScreenC'); }}> | |
<View> | |
<Text>Move to ScreenC ( Scalar notation is also useful to move root level screen )</Text> | |
</View> | |
</TouchableHighlight> | |
<TouchableHighlight onPress={() => { navigateTo(['ScreenD', 'TabScreenB']); }}> | |
<View> | |
<Text>Move to ScreenD->TabScreenB</Text> | |
</View> | |
</TouchableHighlight> | |
} | |
} | |
} | |
export default SomeScreen; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment