소스 검색

subscribe to store only once in App.js

Guy Carmeli 9 년 전
부모
커밋
e2bdad9c36
1개의 변경된 파일8개의 추가작업 그리고 10개의 파일을 삭제
  1. 8
    10
      example-redux/src/app.js

+ 8
- 10
example-redux/src/app.js 파일 보기

17
 // screen related book keeping
17
 // screen related book keeping
18
 import { registerScreens } from './screens';
18
 import { registerScreens } from './screens';
19
 registerScreens(store, Provider);
19
 registerScreens(store, Provider);
20
-let unsubscribe;
21
 
20
 
22
 AppRegistry.registerComponent('ExampleRedux', () => App);
21
 AppRegistry.registerComponent('ExampleRedux', () => App);
23
 
22
 
25
 export default class App extends React.Component {
24
 export default class App extends React.Component {
26
   constructor(props) {
25
   constructor(props) {
27
     super(props);
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
   render() {
37
   render() {
43
     if (this.currentRoot != root) {
46
     if (this.currentRoot != root) {
44
       this.currentRoot = root;
47
       this.currentRoot = root;
45
       this.startApp(root);
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