Bladeren bron

subscribe to store only once in App.js

Guy Carmeli 9 jaren geleden
bovenliggende
commit
e2bdad9c36
1 gewijzigde bestanden met toevoegingen van 8 en 10 verwijderingen
  1. 8
    10
      example-redux/src/app.js

+ 8
- 10
example-redux/src/app.js Bestand weergeven

@@ -17,7 +17,6 @@ const store = createStoreWithMiddleware(reducer);
17 17
 // screen related book keeping
18 18
 import { registerScreens } from './screens';
19 19
 registerScreens(store, Provider);
20
-let unsubscribe;
21 20
 
22 21
 AppRegistry.registerComponent('ExampleRedux', () => App);
23 22
 
@@ -25,10 +24,14 @@ AppRegistry.registerComponent('ExampleRedux', () => App);
25 24
 export default class App extends React.Component {
26 25
   constructor(props) {
27 26
     super(props);
28
-    console.log('constructor');
29
-    // since react-redux only works on components, we need to subscribe this class manually
30
-    unsubscribe = store.subscribe(this.onStoreUpdate.bind(this));
31
-    store.dispatch(appActions.appInitialized());
27
+    console.log('constructor ' + App.created);
28
+    if (!App.created) {
29
+      console.log('store.subscribe');
30
+      // since react-redux only works on components, we need to subscribe this class manually
31
+      store.subscribe(this.onStoreUpdate.bind(this));
32
+      store.dispatch(appActions.appInitialized());
33
+    }
34
+    App.created = true;
32 35
   }
33 36
 
34 37
   render() {
@@ -43,11 +46,6 @@ export default class App extends React.Component {
43 46
     if (this.currentRoot != root) {
44 47
       this.currentRoot = root;
45 48
       this.startApp(root);
46
-    } else {
47
-      if (unsubscribe && this.currentRoot) {
48
-        console.log('unsubscribing ' + this.currentRoot);
49
-        unsubscribe();
50
-      }
51 49
     }
52 50
   }
53 51