react-native-navigation的迁移库

Navigation.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React, { AppRegistry } from 'react-native';
  2. import platformSpecific from './platformSpecific';
  3. import Screen from './Screen';
  4. const registeredScreens = {};
  5. function registerScreen(screenID, generator) {
  6. registeredScreens[screenID] = generator;
  7. AppRegistry.registerComponent(screenID, generator);
  8. }
  9. function registerComponent(screenID, generator, store = undefined, Provider = undefined) {
  10. if (store && Provider) return _registerComponentRedux(screenID, generator, store, Provider);
  11. else return _registerComponentNoRedux(screenID, generator);
  12. }
  13. function _registerComponentNoRedux(screenID, generator) {
  14. const generatorWrapper = function() {
  15. const InternalComponent = generator();
  16. return class extends Screen {
  17. static navigatorStyle = InternalComponent.navigatorStyle || {};
  18. static navigatorButtons = InternalComponent.navigatorButtons || {};
  19. render() {
  20. return (
  21. <InternalComponent navigator={this.navigator} {...this.props} />
  22. );
  23. }
  24. };
  25. }
  26. registerScreen(screenID, generatorWrapper);
  27. }
  28. function _registerComponentRedux(screenID, generator, store, Provider) {
  29. const generatorWrapper = function() {
  30. const InternalComponent = generator();
  31. return class extends Screen {
  32. static navigatorStyle = InternalComponent.navigatorStyle || {};
  33. static navigatorButtons = InternalComponent.navigatorButtons || {};
  34. render() {
  35. return (
  36. <Provider store={store}>
  37. <InternalComponent navigator={this.navigator} {...this.props} />
  38. </Provider>
  39. );
  40. }
  41. };
  42. }
  43. registerScreen(screenID, generatorWrapper);
  44. }
  45. function getRegisteredScreen(screenID) {
  46. const generator = registeredScreens[screenID];
  47. if (!generator) {
  48. console.error('Navigation.getRegisteredScreen: ' + screenID + ' used but not yet registered');
  49. return;
  50. }
  51. return generator();
  52. }
  53. function showModal(params = {}) {
  54. return platformSpecific.showModal(params);
  55. }
  56. function dismissModal(params = {}) {
  57. return platformSpecific.dismissModal(params);
  58. }
  59. export default Navigation = {
  60. registerScreen,
  61. getRegisteredScreen,
  62. registerComponent,
  63. showModal,
  64. dismissModal,
  65. startTabBasedApp: platformSpecific.startTabBasedApp,
  66. startSingleScreenApp: platformSpecific.startSingleScreenApp
  67. }