Advertisement
zidniryi

NavRN.js

Mar 9th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import * as React from 'react';
  2. import { Button, View, Text } from 'react-native';
  3. import { NavigationContainer } from '@react-navigation/native';
  4. import { createStackNavigator } from '@react-navigation/stack';
  5.  
  6. function HomeScreen({ navigation }) {
  7. return (
  8. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  9. <Text>Home Screen</Text>
  10. <Button
  11. title="Go to Details"
  12. onPress={() => navigation.navigate('Details')}
  13. />
  14. <Button
  15. title="Go to Details Params"
  16. onPress={() => {
  17. /* 1. Navigate to the Details route with params */
  18. navigation.navigate('Details', {
  19. itemId: 86,
  20. otherParam: 'anything you want here',
  21. });
  22. }}
  23. />
  24. </View>
  25. );
  26. }
  27.  
  28. function DetailsScreen({ route, navigation }) {
  29. const { itemId } = route.params;
  30. const { otherParam } = route.params;
  31. return (
  32. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  33. <Text>Details Screen</Text>
  34. <Text>itemId: {JSON.stringify(itemId)}</Text>
  35. <Text>otherParam: {JSON.stringify(otherParam)}</Text>
  36. <Button
  37. title="Go to Details... again"
  38. onPress={() => navigation.push('Details')}
  39. />
  40. <Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
  41.  
  42. <Button title="Go back" onPress={() => navigation.goBack()} />
  43. <Button
  44. title="Go back to first screen in stack"
  45. onPress={() => navigation.popToTop()}
  46. />
  47. </View>
  48. );
  49. }
  50.  
  51. const Stack = createStackNavigator();
  52.  
  53. function App() {
  54. return (
  55. <NavigationContainer>
  56. <Stack.Navigator initialRouteName="Home">
  57. <Stack.Screen name="Home" component={HomeScreen} />
  58. <Stack.Screen name="Details" component={DetailsScreen} />
  59. </Stack.Navigator>
  60. </NavigationContainer>
  61. );
  62. }
  63.  
  64. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement