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.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. this.setState({ url: null });
  57. });
  58. }
  59. /**
  60. * Implements React's {@link Component#render()}.
  61. *
  62. * @inheritdoc
  63. * @returns {ReactElement}
  64. */
  65. render() {
  66. // XXX We don't render the App component until we get the initial URL,
  67. // either it's null or some other non-null defined value;
  68. if (typeof this.state.url === 'undefined') {
  69. return null;
  70. }
  71. return (
  72. <App
  73. config = { config }
  74. store = { store }
  75. url = { this.state.url } />
  76. );
  77. }
  78. }
  79. // Register the main Component.
  80. AppRegistry.registerComponent('App', () => Root);