You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

App.native.js 4.0KB

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