StacksWithKeys.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React from 'react';
  2. import { StatusBar, Text, View } from 'react-native';
  3. import { createStackNavigator } from 'react-navigation';
  4. import { Button } from './commonComponents/ButtonWithMargin';
  5. class HomeScreen extends React.Component<any, any> {
  6. render() {
  7. return (
  8. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  9. <Text>Home</Text>
  10. <Button
  11. title="Navigate to 'Profile' with key 'A'"
  12. onPress={() =>
  13. this.props.navigation.navigate({
  14. routeName: 'Profile',
  15. key: 'A',
  16. params: { homeKey: this.props.navigation.state.key },
  17. })
  18. }
  19. />
  20. <Button
  21. title="Go back to other examples"
  22. onPress={() => this.props.navigation.goBack(null)}
  23. />
  24. <StatusBar barStyle="default" />
  25. </View>
  26. );
  27. }
  28. }
  29. class ProfileScreen extends React.Component<any, any> {
  30. render() {
  31. const { homeKey } = this.props.navigation.state.params;
  32. return (
  33. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  34. <Text>Profile</Text>
  35. <Button
  36. title="Navigate to 'Settings' with key 'B'"
  37. onPress={() =>
  38. this.props.navigation.navigate({
  39. routeName: 'Settings',
  40. key: 'B',
  41. params: { homeKey },
  42. })
  43. }
  44. />
  45. <Button
  46. title={`Navigate back to 'Home' with key ${homeKey}`}
  47. onPress={() =>
  48. this.props.navigation.navigate({ routeName: 'Home', key: homeKey })
  49. }
  50. />
  51. </View>
  52. );
  53. }
  54. }
  55. class SettingsScreen extends React.Component<any, any> {
  56. render() {
  57. const { homeKey } = this.props.navigation.state.params;
  58. return (
  59. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  60. <Text>Settings</Text>
  61. <Button
  62. title={`Navigate back to 'Home' with key ${homeKey}`}
  63. onPress={() =>
  64. this.props.navigation.navigate({ routeName: 'Home', key: homeKey })
  65. }
  66. />
  67. <Button
  68. title="Navigate back to 'Profile' with key 'A'"
  69. onPress={() =>
  70. this.props.navigation.navigate({
  71. routeName: 'Profile',
  72. key: 'A',
  73. })
  74. }
  75. />
  76. </View>
  77. );
  78. }
  79. }
  80. const Stack = createStackNavigator(
  81. {
  82. Home: {
  83. screen: HomeScreen,
  84. },
  85. Profile: {
  86. screen: ProfileScreen,
  87. },
  88. Settings: {
  89. screen: SettingsScreen,
  90. },
  91. },
  92. {
  93. headerMode: 'none',
  94. }
  95. );
  96. export default Stack;