Procházet zdrojové kódy

[Doc] The way to unsubscribe from the events (#4505)

Document Navigation.events() unsubscribe
Gang Wang před 6 roky
rodič
revize
565c23a9bc
1 změnil soubory, kde provedl 101 přidání a 27 odebrání
  1. 101
    27
      docs/docs/events.md

+ 101
- 27
docs/docs/events.md Zobrazit soubor

@@ -5,21 +5,36 @@
5 5
 Called once the app is launched. This event is used to set the Application's initial layout - after which the app is ready for user interaction.
6 6
 
7 7
 ```js
8
-Navigation.events().registerAppLaunchedListener(() => {
8
+const appLaunchedListener = Navigation.events().registerAppLaunchedListener(() => {
9 9
 
10 10
 });
11 11
 ```
12 12
 
13
+RNN automatically unsubscribes components when they unmount, therefore unsubscribing isn't actually mandatory if you subscribed in `componentDidMount`.
14
+
15
+But you can use the following method to unsubscribe manually.
16
+
17
+```js
18
+appLaunchedListener.remove();
19
+```
20
+
13 21
 ## componentDidAppear
14 22
 Called each time this component appears on screen (attached to the view hierarchy)
15 23
 
16 24
 ```js
17 25
 class MyComponent extends Component {
18
-  constructor(props) {
19
-    super(props);
20
-    Navigation.events().bindComponent(this);
21
-  }
22 26
 
27
+  componentDidMount() {
28
+    this.navigationEventListener = Navigation.events().bindComponent(this);
29
+  }
30
+  
31
+  componentWillUnmount() {
32
+    // Not mandatory
33
+    if (this.navigationEventListener) {
34
+      this.navigationEventListener.remove();
35
+    }
36
+  }
37
+  
23 38
   componentDidAppear() {
24 39
 
25 40
   }
@@ -29,9 +44,13 @@ class MyComponent extends Component {
29 44
 This event can be observed globally as well:
30 45
 
31 46
 ```js
32
-Navigation.events().registerComponentDidAppearListener(({ componentId, componentName }) => {
47
+// Subscribe
48
+const screenEventListener = Navigation.events().registerComponentDidAppearListener(({ componentId, componentName }) => {
33 49
 
34 50
 });
51
+...
52
+// Unsubscribe
53
+screenEventListener.remove();
35 54
 ```
36 55
 |       Parameter         | Description |
37 56
 |:--------------------:|:-----|
@@ -43,9 +62,16 @@ Called each time this component disappears from screen (detached from the view h
43 62
 
44 63
 ```js
45 64
 class MyComponent extends Component {
46
-  constructor(props) {
47
-    super(props);
48
-    Navigation.events().bindComponent(this);
65
+
66
+  componentDidMount() {
67
+    this.navigationEventListener = Navigation.events().bindComponent(this);
68
+  }
69
+  
70
+  componentWillUnmount() {
71
+    // Not mandatory
72
+    if (this.navigationEventListener) {
73
+      this.navigationEventListener.remove();
74
+    }
49 75
   }
50 76
 
51 77
   componentDidDisappear() {
@@ -57,9 +83,13 @@ class MyComponent extends Component {
57 83
 This event can be observed globally as well:
58 84
 
59 85
 ```js
60
-Navigation.events().registerComponentDidDisappearListener(({ componentId, componentName }) => {
86
+// Subscribe
87
+const screenEventListener = Navigation.events().registerComponentDidDisappearListener(({ componentId, componentName }) => {
61 88
 
62 89
 });
90
+...
91
+// Unsubscribe
92
+screenEventListener.remove();
63 93
 ```
64 94
 |       Parameter         | Description |
65 95
 |:--------------------:|:-----|
@@ -70,9 +100,13 @@ Navigation.events().registerComponentDidDisappearListener(({ componentId, compon
70 100
 The `commandListener` is called whenever a *Navigation command* (i.e push, pop, showModal etc) is invoked.
71 101
 
72 102
 ```js
73
-Navigation.events().registerCommandListener(({ name, params }) => {
103
+// Subscribe
104
+const commandListener = Navigation.events().registerCommandListener(({ name, params }) => {
74 105
 
75 106
 });
107
+...
108
+// Unsubscribe
109
+commandListener.remove();
76 110
 ```
77 111
 |       Parameter         | Description |
78 112
 |:--------------------:|:-----|
@@ -83,9 +117,13 @@ Navigation.events().registerCommandListener(({ name, params }) => {
83 117
 Invoked when a command finishes executing in native. If the command contains animations, for example pushed screen animation,) the listener is invoked after the animation ends.
84 118
 
85 119
 ```js
86
-Navigation.events().registerCommandCompletedListener(({ commandId, completionTime, params }) => {
120
+// Subscribe
121
+const commandCompletedListener = Navigation.events().registerCommandCompletedListener(({ commandId, completionTime, params }) => {
87 122
 
88 123
 });
124
+...
125
+// Unsubscribe
126
+commandCompletedListener.remove();
89 127
 ```
90 128
 
91 129
 |       Parameter         | Description |
@@ -97,9 +135,13 @@ Navigation.events().registerCommandCompletedListener(({ commandId, completionTim
97 135
 Invoked when a BottomTab is selected by the user.
98 136
 
99 137
 ```js
100
-Navigation.events().registerBottomTabSelectedListener(({ selectedTabIndex, unselectedTabIndex }) => {
138
+// Subscribe
139
+const bottomTabEventListener = Navigation.events().registerBottomTabSelectedListener(({ selectedTabIndex, unselectedTabIndex }) => {
101 140
 
102 141
 });
142
+...
143
+// Unsubscribe
144
+bottomTabEventListener.remove();
103 145
 ```
104 146
 
105 147
 |       Parameter         | Description |
@@ -112,9 +154,16 @@ This event is emitted whenever a TopBar button is pressed by the user.
112 154
 
113 155
 ```js
114 156
 class MyComponent extends Component {
115
-  constructor(props) {
116
-    super(props);
117
-    Navigation.events().bindComponent(this);
157
+
158
+  componentDidMount() {
159
+    this.navigationEventListener = Navigation.events().bindComponent(this);
160
+  }
161
+  
162
+  componentWillUnmount() {
163
+    // Not mandatory
164
+    if (this.navigationEventListener) {
165
+      this.navigationEventListener.remove();
166
+    }
118 167
   }
119 168
   
120 169
   navigationButtonPressed({ buttonId }) {
@@ -126,9 +175,13 @@ class MyComponent extends Component {
126 175
 This event can be observed globally as well:
127 176
 
128 177
 ```js
129
-Navigation.events().registerNavigationButtonPressedListener(({ buttonId }) => {
178
+// Subscribe
179
+const navigationButtonEventListener = Navigation.events().registerNavigationButtonPressedListener(({ buttonId }) => {
130 180
 
131 181
 });
182
+...
183
+// Unsubscribe
184
+navigationButtonEventListener.remove();
132 185
 ```
133 186
 
134 187
 |Parameter|Description|
@@ -140,9 +193,16 @@ Called when a SearchBar from NavigationBar gets updated.
140 193
 
141 194
 ```js
142 195
 class MyComponent extends Component {
143
-  constructor(props) {
144
-    super(props);
145
-    Navigation.events().bindComponent(this);
196
+
197
+  componentDidMount() {
198
+    this.navigationEventListener = Navigation.events().bindComponent(this);
199
+  }
200
+  
201
+  componentWillUnmount() {
202
+    // Not mandatory
203
+    if (this.navigationEventListener) {
204
+      this.navigationEventListener.remove();
205
+    }
146 206
   }
147 207
 
148 208
   searchBarUpdated({ text, isFocused }) {
@@ -156,9 +216,16 @@ Called when the cancel button on the SearchBar from NavigationBar gets pressed.
156 216
 
157 217
 ```js
158 218
 class MyComponent extends Component {
159
-  constructor(props) {
160
-    super(props);
161
-    Navigation.events().bindComponent(this);
219
+
220
+  componentDidMount() {
221
+    this.navigationEventListener = Navigation.events().bindComponent(this);
222
+  }
223
+  
224
+  componentWillUnmount() {
225
+    // Not mandatory
226
+    if (this.navigationEventListener) {
227
+      this.navigationEventListener.remove();
228
+    }
162 229
   }
163 230
 
164 231
   searchBarCancelPressed() {
@@ -172,13 +239,20 @@ Called when preview peek is completed
172 239
 
173 240
 ```js
174 241
 class MyComponent extends Component {
175
-  constructor(props) {
176
-    super(props);
177
-    Navigation.events().bindComponent(this);
242
+
243
+  componentDidMount() {
244
+    this.navigationEventListener = Navigation.events().bindComponent(this);
245
+  }
246
+  
247
+  componentWillUnmount() {
248
+    // Not mandatory
249
+    if (this.navigationEventListener) {
250
+      this.navigationEventListener.remove();
251
+    }
178 252
   }
179 253
 
180 254
   previewCompleted({ previewComponentId }) {
181 255
 
182 256
   }
183 257
 }
184
-```
258
+```