react-native-navigation的迁移库

Navigation.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import React from 'react';
  2. import {AppRegistry} from 'react-native';
  3. import platformSpecific from './deprecated/platformSpecificDeprecated';
  4. import Screen from './Screen';
  5. import PropRegistry from './PropRegistry';
  6. const registeredScreens = {};
  7. function registerScreen(screenID, generator) {
  8. registeredScreens[screenID] = generator;
  9. AppRegistry.registerComponent(screenID, generator);
  10. }
  11. function registerComponent(screenID, generator, store = undefined, Provider = undefined) {
  12. if (store && Provider) {
  13. return _registerComponentRedux(screenID, generator, store, Provider);
  14. } else {
  15. return _registerComponentNoRedux(screenID, generator);
  16. }
  17. }
  18. function _registerComponentNoRedux(screenID, generator) {
  19. const generatorWrapper = function() {
  20. const InternalComponent = generator();
  21. return class extends Screen {
  22. static navigatorStyle = InternalComponent.navigatorStyle || {};
  23. static navigatorButtons = InternalComponent.navigatorButtons || {};
  24. constructor(props) {
  25. super(props);
  26. this.state = {
  27. internalProps: {...props, ...PropRegistry.load(props.screenInstanceID)}
  28. }
  29. }
  30. componentWillReceiveProps(nextProps) {
  31. this.setState({
  32. internalProps: {...PropRegistry.load(this.props.screenInstanceID), ...nextProps}
  33. })
  34. }
  35. render() {
  36. return (
  37. <InternalComponent navigator={this.navigator} {...this.state.internalProps} />
  38. );
  39. }
  40. };
  41. };
  42. registerScreen(screenID, generatorWrapper);
  43. return generatorWrapper;
  44. }
  45. function _registerComponentRedux(screenID, generator, store, Provider) {
  46. const generatorWrapper = function() {
  47. const InternalComponent = generator();
  48. return class extends Screen {
  49. static navigatorStyle = InternalComponent.navigatorStyle || {};
  50. static navigatorButtons = InternalComponent.navigatorButtons || {};
  51. constructor(props) {
  52. super(props);
  53. this.state = {
  54. internalProps: {...props, ...PropRegistry.load(props.screenInstanceID)}
  55. }
  56. }
  57. componentWillReceiveProps(nextProps) {
  58. this.setState({
  59. internalProps: {...PropRegistry.load(this.props.screenInstanceID), ...nextProps}
  60. })
  61. }
  62. render() {
  63. return (
  64. <Provider store={store}>
  65. <InternalComponent navigator={this.navigator} {...this.state.internalProps} />
  66. </Provider>
  67. );
  68. }
  69. };
  70. };
  71. registerScreen(screenID, generatorWrapper);
  72. return generatorWrapper;
  73. }
  74. function getRegisteredScreen(screenID) {
  75. const generator = registeredScreens[screenID];
  76. if (!generator) {
  77. console.error(`Navigation.getRegisteredScreen: ${screenID} used but not yet registered`);
  78. return undefined;
  79. }
  80. return generator();
  81. }
  82. function showModal(params = {}) {
  83. return platformSpecific.showModal(params);
  84. }
  85. function dismissModal(params = {}) {
  86. return platformSpecific.dismissModal(params);
  87. }
  88. function dismissAllModals(params = {}) {
  89. return platformSpecific.dismissAllModals(params);
  90. }
  91. function showLightBox(params = {}) {
  92. return platformSpecific.showLightBox(params);
  93. }
  94. function dismissLightBox(params = {}) {
  95. return platformSpecific.dismissLightBox(params);
  96. }
  97. function showInAppNotification(params = {}) {
  98. return platformSpecific.showInAppNotification(params);
  99. }
  100. function dismissInAppNotification(params = {}) {
  101. return platformSpecific.dismissInAppNotification(params);
  102. }
  103. function startTabBasedApp(params) {
  104. return platformSpecific.startTabBasedApp(params);
  105. }
  106. function startSingleScreenApp(params) {
  107. return platformSpecific.startSingleScreenApp(params);
  108. }
  109. export default {
  110. getRegisteredScreen,
  111. registerComponent,
  112. showModal: showModal,
  113. dismissModal: dismissModal,
  114. dismissAllModals: dismissAllModals,
  115. showLightBox: showLightBox,
  116. dismissLightBox: dismissLightBox,
  117. showInAppNotification: showInAppNotification,
  118. dismissInAppNotification: dismissInAppNotification,
  119. startTabBasedApp: startTabBasedApp,
  120. startSingleScreenApp: startSingleScreenApp
  121. };