Browse Source

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

Document Navigation.events() unsubscribe
Gang Wang 5 years ago
parent
commit
565c23a9bc
1 changed files with 101 additions and 27 deletions
  1. 101
    27
      docs/docs/events.md

+ 101
- 27
docs/docs/events.md View File

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.
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
 ```js
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
 ## componentDidAppear
21
 ## componentDidAppear
14
 Called each time this component appears on screen (attached to the view hierarchy)
22
 Called each time this component appears on screen (attached to the view hierarchy)
15
 
23
 
16
 ```js
24
 ```js
17
 class MyComponent extends Component {
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
   componentDidAppear() {
38
   componentDidAppear() {
24
 
39
 
25
   }
40
   }
29
 This event can be observed globally as well:
44
 This event can be observed globally as well:
30
 
45
 
31
 ```js
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
 |       Parameter         | Description |
55
 |       Parameter         | Description |
37
 |:--------------------:|:-----|
56
 |:--------------------:|:-----|
43
 
62
 
44
 ```js
63
 ```js
45
 class MyComponent extends Component {
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
   componentDidDisappear() {
77
   componentDidDisappear() {
57
 This event can be observed globally as well:
83
 This event can be observed globally as well:
58
 
84
 
59
 ```js
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
 |       Parameter         | Description |
94
 |       Parameter         | Description |
65
 |:--------------------:|:-----|
95
 |:--------------------:|:-----|
70
 The `commandListener` is called whenever a *Navigation command* (i.e push, pop, showModal etc) is invoked.
100
 The `commandListener` is called whenever a *Navigation command* (i.e push, pop, showModal etc) is invoked.
71
 
101
 
72
 ```js
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
 |       Parameter         | Description |
111
 |       Parameter         | Description |
78
 |:--------------------:|:-----|
112
 |:--------------------:|:-----|
83
 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.
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
 ```js
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
 |       Parameter         | Description |
129
 |       Parameter         | Description |
97
 Invoked when a BottomTab is selected by the user.
135
 Invoked when a BottomTab is selected by the user.
98
 
136
 
99
 ```js
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
 |       Parameter         | Description |
147
 |       Parameter         | Description |
112
 
154
 
113
 ```js
155
 ```js
114
 class MyComponent extends Component {
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
   navigationButtonPressed({ buttonId }) {
169
   navigationButtonPressed({ buttonId }) {
126
 This event can be observed globally as well:
175
 This event can be observed globally as well:
127
 
176
 
128
 ```js
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
 |Parameter|Description|
187
 |Parameter|Description|
140
 
193
 
141
 ```js
194
 ```js
142
 class MyComponent extends Component {
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
   searchBarUpdated({ text, isFocused }) {
208
   searchBarUpdated({ text, isFocused }) {
156
 
216
 
157
 ```js
217
 ```js
158
 class MyComponent extends Component {
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
   searchBarCancelPressed() {
231
   searchBarCancelPressed() {
172
 
239
 
173
 ```js
240
 ```js
174
 class MyComponent extends Component {
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
   previewCompleted({ previewComponentId }) {
254
   previewCompleted({ previewComponentId }) {
181
 
255
 
182
   }
256
   }
183
 }
257
 }
184
-```
258
+```