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.

index.native.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React, { Component } from 'react';
  2. import { AppRegistry, Linking } from 'react-native';
  3. import { createStore } from 'redux';
  4. import Thunk from 'redux-thunk';
  5. import config from './config';
  6. import { App } from './features/app';
  7. import {
  8. MiddlewareRegistry,
  9. ReducerRegistry
  10. } from './features/base/redux';
  11. // Create combined reducer from all reducers in registry.
  12. const reducer = ReducerRegistry.combineReducers();
  13. // Apply all registered middleware from the MiddlewareRegistry + additional
  14. // 3rd party middleware:
  15. // - Thunk - allows us to dispatch async actions easily. For more info
  16. // @see https://github.com/gaearon/redux-thunk.
  17. const middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  18. // Create Redux store with our reducer and middleware.
  19. const store = createStore(reducer, middleware);
  20. /**
  21. * React Native doesn't support specifying props to the main/root component (in
  22. * the JS/JSX source code). So create a wrapper React Component (class) around
  23. * features/app's App instead.
  24. *
  25. * @extends Component
  26. */
  27. class Root extends Component {
  28. /**
  29. * Initializes a new Root instance.
  30. *
  31. * @param {Object} props - The read-only properties with which the new
  32. * instance is to be initialized.
  33. */
  34. constructor(props) {
  35. super(props);
  36. /**
  37. * The initial state of this Component.
  38. *
  39. * @type {{url: string}}
  40. */
  41. this.state = {
  42. /**
  43. * The URL, if any, with which the app was launched.
  44. *
  45. * @type {string}
  46. */
  47. url: undefined
  48. };
  49. // Handle the URL, if any, with which the app was launched.
  50. Linking.getInitialURL()
  51. .then(url => this.setState({ url }))
  52. .catch(err => {
  53. console.error('Failed to get initial URL', err);
  54. // XXX Start with an empty URL if getting the initial URL fails;
  55. // otherwise, nothing will be rendered.
  56. if (this.state.url !== null) {
  57. this.setState({ url: null });
  58. }
  59. });
  60. }
  61. /**
  62. * Implements React's {@link Component#render()}.
  63. *
  64. * @inheritdoc
  65. * @returns {ReactElement}
  66. */
  67. render() {
  68. // XXX We don't render the App component until we get the initial URL,
  69. // either it's null or some other non-null defined value;
  70. if (typeof this.state.url === 'undefined') {
  71. return null;
  72. }
  73. return (
  74. <App
  75. config = { config }
  76. store = { store }
  77. url = { this.state.url } />
  78. );
  79. }
  80. }
  81. // Register the main Component.
  82. AppRegistry.registerComponent('App', () => Root);