Bladeren bron

addOnNavigatorEvent improvements (#2175)

* throwing an error instead of a string

* return a callback to remove the register handler
dror biran 6 jaren geleden
bovenliggende
commit
02aee567fa
2 gewijzigde bestanden met toevoegingen van 14 en 3 verwijderingen
  1. 8
    0
      docs/screen-api.md
  2. 6
    3
      src/Screen.js

+ 8
- 0
docs/screen-api.md Bestand weergeven

@@ -259,12 +259,20 @@ this.props.navigator.toggleNavBar({
259 259
 ## setOnNavigatorEvent(callback)
260 260
 
261 261
 Set a handler for navigator events (like nav button press). This would normally go in your component constructor.
262
+Can not be used in conjuction with `addOnNavigatorEvent`.
262 263
 
263 264
 ```js
264 265
 // this.onNavigatorEvent will be our handler
265 266
 this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
266 267
 ```
267 268
 
269
+## addOnNavigatorEvent(callback)
270
+
271
+Add a handler for navigator events (like nav button press). This would normally go in your component constructor.
272
+If you choose to use `addOnNavigatorEvent` instead of `setOnNavigatorEvent` you will be able to add multiple handlers.
273
+Bear in mind that you can't use both `addOnNavigatorEvent` and `setOnNavigatorEvent`.
274
+`addOnNavigatorEvent` returns a function, that once called will remove the registered handler.
275
+
268 276
 # Screen Visibility
269 277
 
270 278
 `const isVisible = await this.props.navigator.screenIsCurrentlyVisible()`

+ 6
- 3
src/Screen.js Bestand weergeven

@@ -143,7 +143,7 @@ class Navigator {
143 143
 
144 144
   setOnNavigatorEvent(callback) {
145 145
     if (this.navigatorEventHandlers.length > 0) {
146
-      throw 'setOnNavigatorEvent can not be used after addOnNavigatorEvent has been called';
146
+      throw new Error('setOnNavigatorEvent can not be used after addOnNavigatorEvent has been called');
147 147
     }
148 148
     this.navigatorEventHandler = callback;
149 149
     this._registerNavigatorEvent();
@@ -151,12 +151,15 @@ class Navigator {
151 151
 
152 152
   addOnNavigatorEvent(callback) {
153 153
     if (this.navigatorEventHandler) {
154
-      throw 'addOnNavigatorEvent can not be used after setOnNavigatorEvent has been called';
154
+      throw new Error('addOnNavigatorEvent can not be used after setOnNavigatorEvent has been called');
155 155
     }
156 156
     if (this.navigatorEventHandlers.indexOf(callback) === -1) {
157 157
       this.navigatorEventHandlers.push(callback);
158 158
     }
159 159
     this._registerNavigatorEvent();
160
+
161
+    return () => this._removeOnNavigatorEvent(callback)
162
+    
160 163
   }
161 164
 
162 165
   _registerNavigatorEvent() {
@@ -167,7 +170,7 @@ class Navigator {
167 170
     }
168 171
   }
169 172
 
170
-  removeOnNavigatorEvent(callback) {
173
+  _removeOnNavigatorEvent(callback) {
171 174
     const index = this.navigatorEventHandlers.indexOf(callback);
172 175
     if (index !== -1) {
173 176
       this.navigatorEventHandlers.splice(index, 1);