react-native-navigation的迁移库

SetRootScreen.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const React = require('react');
  2. const Root = require('../components/Root');
  3. const Button = require('../components/Button')
  4. const Navigation = require('./../services/Navigation');
  5. const {
  6. NAVIGATION_TAB,
  7. SET_MULTIPLE_ROOTS_BTN,
  8. SET_ROOT_BTN,
  9. LAYOUTS_TAB,
  10. SET_ROOT_HIDES_BOTTOM_TABS_BTN,
  11. SET_ROOT_WITH_STACK_HIDES_BOTTOM_TABS_BTN
  12. } = require('../testIDs');
  13. const Screens = require('./Screens');
  14. const { logLifecycleEvent } = require('./StaticLifecycleOverlay');
  15. let unmounted;
  16. class SetRootScreen extends React.Component {
  17. static options() {
  18. return {
  19. topBar: {
  20. title: {
  21. text: 'Navigation'
  22. }
  23. },
  24. bottomTab: {
  25. text: 'Navigation',
  26. icon: require('../../img/navigation.png'),
  27. testID: NAVIGATION_TAB
  28. }
  29. };
  30. }
  31. constructor(props) {
  32. super(props);
  33. unmounted = false;
  34. }
  35. render() {
  36. return (
  37. <Root componentId={this.props.componentId}>
  38. <Button label='Set Root' testID={SET_ROOT_BTN} onPress={this.setSingleRoot} />
  39. <Button label='Set Multiple Roots' testID={SET_MULTIPLE_ROOTS_BTN} onPress={this.setMultipleRoot} />
  40. <Button label='Set Root - hides bottomTabs' testID={SET_ROOT_HIDES_BOTTOM_TABS_BTN} onPress={this.setRootHidesBottomTabs} />
  41. <Button label='Set Root with stack - hides bottomTabs' testID={SET_ROOT_WITH_STACK_HIDES_BOTTOM_TABS_BTN} onPress={this.setRootWithStackHidesBottomTabs} />
  42. </Root>
  43. );
  44. }
  45. componentWillUnmount() {
  46. unmounted = true;
  47. }
  48. setSingleRoot = async () => {
  49. await this.setRoot();
  50. logLifecycleEvent({text: `setRoot complete - previous root is${unmounted ? '' : ' not'} unmounted`});
  51. }
  52. setMultipleRoot = async () => {
  53. await this.setRoot();
  54. await this.setRoot();
  55. };
  56. setRoot = async () => await Navigation.setRoot({
  57. root: {
  58. stack: {
  59. id: 'stack',
  60. children: [{
  61. component: {
  62. id: 'component',
  63. name: Screens.Pushed
  64. }
  65. }]
  66. }
  67. }
  68. });
  69. setRootHidesBottomTabs = async () => await Navigation.setRoot({
  70. root: {
  71. bottomTabs: {
  72. children: [{
  73. stack: {
  74. id: 'stack',
  75. children: [{
  76. component: {
  77. id: 'component',
  78. name: Screens.Pushed,
  79. options: {
  80. bottomTabs: {
  81. visible: false
  82. }
  83. }
  84. }
  85. }]
  86. }
  87. }],
  88. options: {
  89. bottomTabs: {
  90. testID: LAYOUTS_TAB
  91. }
  92. }
  93. }
  94. }
  95. });
  96. setRootWithStackHidesBottomTabs = async () => await Navigation.setRoot({
  97. root: {
  98. bottomTabs: {
  99. children: [{
  100. stack: {
  101. id: 'stack',
  102. children: [{
  103. component: {
  104. id: 'component',
  105. name: Screens.Pushed
  106. }
  107. },
  108. {
  109. component: {
  110. id: 'component2',
  111. name: Screens.Pushed,
  112. options: {
  113. bottomTabs: {
  114. visible: false
  115. }
  116. }
  117. }
  118. }]
  119. }
  120. }],
  121. options: {
  122. bottomTabs: {
  123. testID: LAYOUTS_TAB
  124. }
  125. }
  126. }
  127. }
  128. });
  129. }
  130. module.exports = SetRootScreen;