Skip to content

Commit c251112

Browse files
author
jwngr
committed
Fix bug with unbinding when component is unmounted
1 parent 2ceda03 commit c251112

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
@@ -0,0 +1 @@
1+
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
@@ -1105,5 +1105,40 @@ describe('ReactFire', function() {
11051105

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

0 commit comments

Comments
 (0)