Skip to content

Instantly share code, notes, and snippets.

@shinchit
Last active October 10, 2019 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinchit/273309eba7bad4880fb654434a02ddd4 to your computer and use it in GitHub Desktop.
Save shinchit/273309eba7bad4880fb654434a02ddd4 to your computer and use it in GitHub Desktop.
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