Skip to content

Commit a3ced1f

Browse files
committed
Allow multiple listeners for identical paths
The current version of ReactFire doesn't cope well with listening to the same path in multiple components. This is because the listener reference from `ref.on('value')` is never stored, and thus all listeners are removed when `ref.off('value')` is called without a listener parameter. This patch stores each `ref.on('value')` listener and passes it to `ref.off('value', listener)`.
1 parent 623ae0f commit a3ced1f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/ReactFireMixin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var ReactFireMixin = {
55
/* Initializes the Firebase binding refs array */
66
componentWillMount: function() {
77
this.firebaseRefs = {};
8+
this.firebaseListeners = {};
89
},
910

1011
/* Removes any remaining Firebase bindings */
@@ -47,7 +48,7 @@ var ReactFireMixin = {
4748
}
4849

4950
this.firebaseRefs[bindVar] = firebaseRef.ref();
50-
firebaseRef.on("value", function(dataSnapshot) {
51+
this.firebaseListeners[bindVar] = firebaseRef.on("value", function(dataSnapshot) {
5152
var newState = {};
5253
if (bindAsArray) {
5354
newState[bindVar] = this._toArray(dataSnapshot.val());
@@ -67,8 +68,9 @@ var ReactFireMixin = {
6768
throw new Error("unexpected value for bindVar. \"" + bindVar + "\" was either never bound or has already been unbound");
6869
}
6970

70-
this.firebaseRefs[bindVar].off("value");
71+
this.firebaseRefs[bindVar].off("value", this.firebaseListeners[bindVar]);
7172
delete this.firebaseRefs[bindVar];
73+
delete this.firebaseListeners[bindVar];
7274
},
7375

7476

@@ -122,4 +124,4 @@ var ReactFireMixin = {
122124
}
123125
return out;
124126
}
125-
};
127+
};

0 commit comments

Comments
 (0)