您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

App.native.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React from 'react';
  2. import { Linking, Navigator } from 'react-native';
  3. import { Provider } from 'react-redux';
  4. import { _getRouteToRender } from '../functions';
  5. import { AbstractApp } from './AbstractApp';
  6. /**
  7. * Root application component.
  8. *
  9. * @extends AbstractApp
  10. */
  11. export class App extends AbstractApp {
  12. /**
  13. * App component's property types.
  14. *
  15. * @static
  16. */
  17. static propTypes = AbstractApp.propTypes
  18. /**
  19. * Initializes a new App instance.
  20. *
  21. * @param {Object} props - The read-only React Component props with which
  22. * the new instance is to be initialized.
  23. */
  24. constructor(props) {
  25. super(props);
  26. // Bind event handlers so they are only bound once for every instance.
  27. this._navigatorRenderScene = this._navigatorRenderScene.bind(this);
  28. this._onLinkingURL = this._onLinkingURL.bind(this);
  29. }
  30. /**
  31. * Subscribe to notifications about activating URLs registered to be handled
  32. * by this app.
  33. *
  34. * @inheritdoc
  35. * @see https://facebook.github.io/react-native/docs/linking.html
  36. * @returns {void}
  37. */
  38. componentWillMount() {
  39. super.componentWillMount();
  40. Linking.addEventListener('url', this._onLinkingURL);
  41. }
  42. /**
  43. * Unsubscribe from notifications about activating URLs registered to be
  44. * handled by this app.
  45. *
  46. * @inheritdoc
  47. * @see https://facebook.github.io/react-native/docs/linking.html
  48. * @returns {void}
  49. */
  50. componentWillUnmount() {
  51. Linking.removeEventListener('url', this._onLinkingURL);
  52. super.componentWillUnmount();
  53. }
  54. /**
  55. * Implements React's {@link Component#render()}.
  56. *
  57. * @inheritdoc
  58. * @returns {ReactElement}
  59. */
  60. render() {
  61. const store = this.props.store;
  62. /* eslint-disable brace-style, react/jsx-no-bind */
  63. return (
  64. <Provider store = { store }>
  65. <Navigator
  66. initialRoute = { _getRouteToRender(store.getState) }
  67. ref = { navigator => { this.navigator = navigator; } }
  68. renderScene = { this._navigatorRenderScene } />
  69. </Provider>
  70. );
  71. /* eslint-enable brace-style, react/jsx-no-bind */
  72. }
  73. /**
  74. * Navigates to a specific Route (via platform-specific means).
  75. *
  76. * @param {Route} route - The Route to which to navigate.
  77. * @returns {void}
  78. */
  79. _navigate(route) {
  80. const navigator = this.navigator;
  81. // TODO Currently, the replace method doesn't support animation. Work
  82. // towards adding it is done in
  83. // https://github.com/facebook/react-native/issues/1981
  84. // XXX React Native's Navigator adds properties to the route it's
  85. // provided with. Clone the specified route in order to prevent its
  86. // modification.
  87. navigator && navigator.replace({ ...route });
  88. }
  89. /**
  90. * Renders the scene identified by a specific route in the Navigator of this
  91. * instance.
  92. *
  93. * @param {Object} route - The route which identifies the scene to be
  94. * rendered in the associated Navigator. In the fashion of NavigatorIOS, the
  95. * specified route is expected to define a value for its component property
  96. * which is the type of React component to be rendered.
  97. * @private
  98. * @returns {ReactElement}
  99. */
  100. _navigatorRenderScene(route) {
  101. // We started with NavigatorIOS and then switched to Navigator in order
  102. // to support Android as well. In order to reduce the number of
  103. // modifications, accept the same format of route definition.
  104. return this._createElement(route.component, {});
  105. }
  106. /**
  107. * Notified by React's Linking API that a specific URL registered to be
  108. * handled by this App was activated.
  109. *
  110. * @param {Object} event - The details of the notification/event.
  111. * @param {string} event.url - The URL registered to be handled by this App
  112. * which was activated.
  113. * @private
  114. * @returns {void}
  115. */
  116. _onLinkingURL(event) {
  117. this._openURL(event.url);
  118. }
  119. }