react-native-navigation的迁移库

docs-root.mdx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ---
  2. id: docs-root
  3. title: Root
  4. sidebar_label: Root
  5. ---
  6. import Tabs from '@theme/Tabs';
  7. import TabItem from '@theme/TabItem';
  8. The root is where the application layout structure is defined. It is typically the first UI element a user interacts with. The root can be changed multiple times during the lifespan of the application. For example, if an app requires users to login, it's common to use a stack-based root and transition to either tabs- or SideMenu-based root if login is successful.
  9. ## Setting root on app launch
  10. RNN exposes an appLaunched listener which is invoked whenever root needs to be set.
  11. On iOS, the app launched event is usually emitted once in the lifespan of the application - when the app is opened for the first time. On Android things become a little bit more complicated. Like on iOS, the event is emitted when an app is opened for the first time. Since the system can destroy the Activity when the app is in the background to free resources, the event is emitted when the app returns to foreground after the activity has been destroyed.
  12. ```js
  13. Navigation.events().registerAppLaunchedListener(() => {
  14. Navigation.setRoot({
  15. root: {
  16. }
  17. });
  18. });
  19. ```
  20. :::important
  21. registerAppLaunchedListener() must be called as soon as the bundle is executed. Otherwise the event, which is emitted from native to JS, won't be handled at the correct moment in time.
  22. ::::
  23. ## Conditional initial root
  24. A common use case is to set the initial root according to a condition of some sort. For example:
  25. > If a user is logged in, show the application main root; otherwise show a login screen.
  26. To do this, simply set the appropriate root according to your needs.
  27. ```js
  28. Navigation.events().registerAppLaunchedListener(() => {
  29. if (isUserLoggedIn()) {
  30. setMainRoot();
  31. } else {
  32. setLoginRoot();
  33. }
  34. });
  35. ```
  36. ## Common root structures
  37. <Tabs
  38. defaultValue='stack'
  39. values={[
  40. { label: 'Stack root', value: 'stack' },
  41. { label: 'BottomTabs root', value: 'bottomTabs' },
  42. { label: 'SideMenu root', value: 'sideMenu' }
  43. ]
  44. }>
  45. <TabItem value='stack'>
  46. Stacks are usually used as root for small scale apps or for short-lived flows within one big app. For example, here's a login flow:
  47. ```js
  48. Navigation.setRoot({
  49. root: {
  50. stack: {
  51. children: [
  52. {
  53. component: {
  54. name: 'LOGIN_SCREEN'
  55. }
  56. }
  57. ]
  58. }
  59. }
  60. });
  61. ```
  62. </TabItem>
  63. <TabItem value='bottomTabs'>
  64. Typically, stacks are used as direct children of BottomTabs and each child is an independent root. This lets users seamlessly switch between tabs as each tab has its own navigation hierarchy.
  65. ```js
  66. Navigation.setRoot({
  67. root: {
  68. bottomTabs: {
  69. children: [
  70. stack: {
  71. children: [
  72. {
  73. component: {
  74. name: 'FEED_SCREEN'
  75. }
  76. }
  77. ]
  78. },
  79. stack: {
  80. children: [
  81. {
  82. component: {
  83. name: 'CHAT_LIST'
  84. }
  85. }
  86. ]
  87. },
  88. stack: {
  89. children: [
  90. {
  91. component: {
  92. name: 'PROFILE_SCREEN'
  93. }
  94. }
  95. ]
  96. }
  97. ]
  98. }
  99. }
  100. });
  101. ```
  102. </TabItem>
  103. <TabItem value='sideMenu'>
  104. When a SideMenu layout is used as root, the center screen would typically be a stack. The center stack should be treated as a root - you can push child screens into it, or replace it alltogether by calling `setStackRoot`.
  105. ```js
  106. Navigation.setRoot({
  107. root: {
  108. sideMenu: {
  109. center: {
  110. stack: {
  111. children: [
  112. name: 'HOME_SCREEN'
  113. ]
  114. }
  115. },
  116. left: {
  117. component: {
  118. name: 'MENU_SCREEN'
  119. }
  120. }
  121. }
  122. }
  123. });
  124. ```
  125. </TabItem>
  126. </Tabs>