Skip to content

Commit bda582b

Browse files
committed
Unsubscribe only if there is a subscription. Fixes #28
1 parent cd8311d commit bda582b

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Diff for: src/components/createConnect.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default function createConnect(React) {
5454

5555
shouldComponentUpdate(nextProps, nextState) {
5656
return (
57-
this.subscribed &&
57+
this.isSubscribed() &&
5858
!areStatePropsEqual(this.state.stateProps, nextState.stateProps)
5959
) || !shallowEqualScalar(this.props, nextProps);
6060
}
@@ -68,15 +68,18 @@ export default function createConnect(React) {
6868
};
6969
}
7070

71+
isSubscribed() {
72+
return typeof this.unsubscribe === 'function';
73+
}
74+
7175
componentDidMount() {
7276
if (shouldSubscribe) {
73-
this.subscribed = true;
7477
this.unsubscribe = this.context.store.subscribe(::this.handleChange);
7578
}
7679
}
7780

7881
componentWillUnmount() {
79-
if (shouldSubscribe) {
82+
if (this.isSubscribed()) {
8083
this.unsubscribe();
8184
}
8285
}

Diff for: test/components/connect.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ describe('React', () => {
241241
TestUtils.findRenderedComponentWithType(container, Container)
242242
).toNotThrow();
243243
const decorated = TestUtils.findRenderedComponentWithType(container, Container);
244-
expect(decorated.subscribed).toBe(true);
244+
expect(decorated.isSubscribed()).toBe(true);
245245
});
246246

247247
it('should not subscribe to stores if mapState argument is falsy', () => {
@@ -268,7 +268,7 @@ describe('React', () => {
268268
TestUtils.findRenderedComponentWithType(container, Container)
269269
).toNotThrow();
270270
const decorated = TestUtils.findRenderedComponentWithType(container, Container);
271-
expect(decorated.subscribed).toNotBe(true);
271+
expect(decorated.isSubscribed()).toNotBe(true);
272272
});
273273

274274
it('should unsubscribe before unmounting', () => {

0 commit comments

Comments
 (0)