Skip to content

Commit 270a1a7

Browse files
author
Jacob Wenger
committed
Merge pull request #69 from firebase/jw-unbind-fix
Fix bug with unbinding when component is unmounted
2 parents 55a3468 + 64d9885 commit 270a1a7

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
feature - Upgraded Firebase dependency to 2.x.x.
2+
fixed - Fixed bug which caused `unbind()` to be called on a previously unbound reference when a component was unmounted.

src/reactfire.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@
351351
this.firebaseRefs[bindVar].off(event, offListener);
352352
}
353353
}
354-
this.firebaseRefs[bindVar] = undefined;
355-
this.firebaseListeners[bindVar] = undefined;
354+
delete this.firebaseRefs[bindVar];
355+
delete this.firebaseListeners[bindVar];
356356

357357
// Update state
358358
var newState = {};

tests/reactfire.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,5 +1104,40 @@ describe('ReactFire', function() {
11041104

11051105
shallowRenderer.render(React.createElement(TestComponent));
11061106
});
1107+
1108+
it('handles already unbound state when the component unmounts', function(done) {
1109+
var TestComponent = React.createClass({
1110+
mixins: [ReactFireMixin],
1111+
1112+
componentWillMount: function() {
1113+
sinon.spy(this, 'unbind');
1114+
1115+
this.bindAsArray(firebaseRef, 'items0');
1116+
this.bindAsObject(firebaseRef, 'items1');
1117+
1118+
firebaseRef.set({
1119+
first: { index: 0 },
1120+
second: { index: 1 },
1121+
third: { index: 2 }
1122+
}, function() {
1123+
this.unbind('items0');
1124+
1125+
shallowRenderer.unmount();
1126+
1127+
expect(this.unbind).to.have.been.calledTwice;
1128+
expect(this.unbind.args[0][0]).to.equal('items0');
1129+
expect(this.unbind.args[1][0]).to.equal('items1');
1130+
1131+
done();
1132+
}.bind(this));
1133+
},
1134+
1135+
render: function() {
1136+
return React.DOM.div(null);
1137+
}
1138+
});
1139+
1140+
shallowRenderer.render(React.createElement(TestComponent));
1141+
});
11071142
});
11081143
});

0 commit comments

Comments
 (0)