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

App.native.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* global __DEV__ */
  2. import React from 'react';
  3. import { Linking, Navigator } from 'react-native';
  4. import { Provider } from 'react-redux';
  5. import { Platform } from '../../base/react';
  6. import { _getRouteToRender } from '../functions';
  7. import { AbstractApp } from './AbstractApp';
  8. /**
  9. * Root application component.
  10. *
  11. * @extends AbstractApp
  12. */
  13. export class App extends AbstractApp {
  14. /**
  15. * App component's property types.
  16. *
  17. * @static
  18. */
  19. static propTypes = AbstractApp.propTypes
  20. /**
  21. * Initializes a new App instance.
  22. *
  23. * @param {Object} props - The read-only React Component props with which
  24. * the new instance is to be initialized.
  25. */
  26. constructor(props) {
  27. super(props);
  28. // Bind event handlers so they are only bound once for every instance.
  29. this._navigatorRenderScene = this._navigatorRenderScene.bind(this);
  30. this._onLinkingURL = this._onLinkingURL.bind(this);
  31. // In the Release configuration, React Native will (intentionally) throw
  32. // an unhandled JavascriptException for an unhandled JavaScript error.
  33. // This will effectively kill the application. In accord with the Web,
  34. // do not kill the application.
  35. this._maybeDisableExceptionsManager();
  36. }
  37. /**
  38. * Subscribe to notifications about activating URLs registered to be handled
  39. * by this app.
  40. *
  41. * @inheritdoc
  42. * @see https://facebook.github.io/react-native/docs/linking.html
  43. * @returns {void}
  44. */
  45. componentWillMount() {
  46. super.componentWillMount();
  47. Linking.addEventListener('url', this._onLinkingURL);
  48. }
  49. /**
  50. * Unsubscribe from notifications about activating URLs registered to be
  51. * handled by this app.
  52. *
  53. * @inheritdoc
  54. * @see https://facebook.github.io/react-native/docs/linking.html
  55. * @returns {void}
  56. */
  57. componentWillUnmount() {
  58. Linking.removeEventListener('url', this._onLinkingURL);
  59. super.componentWillUnmount();
  60. }
  61. /**
  62. * Implements React's {@link Component#render()}.
  63. *
  64. * @inheritdoc
  65. * @returns {ReactElement}
  66. */
  67. render() {
  68. const store = this.props.store;
  69. /* eslint-disable brace-style, react/jsx-no-bind */
  70. return (
  71. <Provider store = { store }>
  72. <Navigator
  73. initialRoute = { _getRouteToRender(store.getState) }
  74. ref = { navigator => { this.navigator = navigator; } }
  75. renderScene = { this._navigatorRenderScene } />
  76. </Provider>
  77. );
  78. /* eslint-enable brace-style, react/jsx-no-bind */
  79. }
  80. /**
  81. * Navigates to a specific Route (via platform-specific means).
  82. *
  83. * @param {Route} route - The Route to which to navigate.
  84. * @returns {void}
  85. */
  86. _navigate(route) {
  87. const navigator = this.navigator;
  88. // TODO Currently, the replace method doesn't support animation. Work
  89. // towards adding it is done in
  90. // https://github.com/facebook/react-native/issues/1981
  91. // XXX React Native's Navigator adds properties to the route it's
  92. // provided with. Clone the specified route in order to prevent its
  93. // modification.
  94. navigator && navigator.replace({ ...route });
  95. }
  96. /**
  97. * Attempts to disable the use of React Native
  98. * {@link ExceptionsManager#handleException} on platforms and in
  99. * configurations on/in which the use of the method in questions has been
  100. * determined to be undesirable. For example, React Native will
  101. * (intentionally) throw an unhandled JavascriptException for an
  102. * unhandled JavaScript error in the Release configuration. This will
  103. * effectively kill the application. In accord with the Web, do not kill the
  104. * application.
  105. *
  106. * @private
  107. * @returns {void}
  108. */
  109. _maybeDisableExceptionsManager() {
  110. if (__DEV__) {
  111. // As mentioned above, only the Release configuration was observed
  112. // to suffer.
  113. return;
  114. }
  115. if (Platform.OS !== 'android') {
  116. // A solution based on RTCSetFatalHandler was implemented on iOS and
  117. // it is preferred because it is at a later step of the
  118. // error/exception handling and it is specific to fatal
  119. // errors/exceptions which were observed to kill the application.
  120. // The solution implemented bellow was tested on Android only so it
  121. // is considered safest to use it there only.
  122. return;
  123. }
  124. const oldHandler = global.ErrorUtils.getGlobalHandler();
  125. const newHandler = _handleException;
  126. if (!oldHandler || oldHandler !== newHandler) {
  127. newHandler.next = oldHandler;
  128. global.ErrorUtils.setGlobalHandler(newHandler);
  129. }
  130. }
  131. /**
  132. * Renders the scene identified by a specific route in the Navigator of this
  133. * instance.
  134. *
  135. * @param {Object} route - The route which identifies the scene to be
  136. * rendered in the associated Navigator. In the fashion of NavigatorIOS, the
  137. * specified route is expected to define a value for its component property
  138. * which is the type of React component to be rendered.
  139. * @private
  140. * @returns {ReactElement}
  141. */
  142. _navigatorRenderScene(route) {
  143. // We started with NavigatorIOS and then switched to Navigator in order
  144. // to support Android as well. In order to reduce the number of
  145. // modifications, accept the same format of route definition.
  146. return this._createElement(route.component, {});
  147. }
  148. /**
  149. * Notified by React's Linking API that a specific URL registered to be
  150. * handled by this App was activated.
  151. *
  152. * @param {Object} event - The details of the notification/event.
  153. * @param {string} event.url - The URL registered to be handled by this App
  154. * which was activated.
  155. * @private
  156. * @returns {void}
  157. */
  158. _onLinkingURL(event) {
  159. this._openURL(event.url);
  160. }
  161. }
  162. /**
  163. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  164. * from converting a fatal error into an unhandled native exception which will
  165. * kill the application.
  166. *
  167. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  168. * @param {boolean} fatal - True if the specified error is fatal; otherwise,
  169. * false.
  170. * @private
  171. * @returns {void}
  172. */
  173. function _handleException(error, fatal) {
  174. if (fatal) {
  175. // In the Release configuration, React Native will (intentionally) throw
  176. // an unhandled JavascriptException for an unhandled JavaScript error.
  177. // This will effectively kill the application. In accord with the Web,
  178. // do not kill the application.
  179. console.error(error);
  180. } else {
  181. // Forward to the next globalHandler of ErrorUtils.
  182. const next = _handleException.next;
  183. typeof next === 'function' && next(error, fatal);
  184. }
  185. }