Skip to content

Don't set state if unmounted during dispatch #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"mocha-jsdom": "~0.4.0",
"react": "^0.14.0-beta3",
"react-addons-test-utils": "^0.14.0-beta3",
"react-dom": "^0.14.0-beta3",
"redux": "^2.0.0",
"rimraf": "^2.3.4",
"webpack": "^1.11.0"
Expand Down
4 changes: 4 additions & 0 deletions src/components/createConnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ export default function createConnect(React) {
}

handleChange() {
if (!this.unsubscribe) {
return;
}

if (this.updateStateProps()) {
this.updateState();
}
Expand Down
45 changes: 40 additions & 5 deletions test/components/connect.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import expect from 'expect';
import jsdom from 'mocha-jsdom';
import React, { createClass, Children, PropTypes, Component } from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import { createStore } from 'redux';
import { connect } from '../../src/index';
Expand Down Expand Up @@ -643,7 +644,7 @@ describe('React', () => {
};

@connect(
state => ({string: state}),
state => ({ string: state }),
dispatch => ({ dispatch })
)
class Container extends Component {
Expand All @@ -652,18 +653,52 @@ describe('React', () => {
}
}

const tree = TestUtils.renderIntoDocument(
const div = document.createElement('div');
ReactDOM.render(
<ProviderMock store={store}>
<Container />
</ProviderMock>
</ProviderMock>,
div
);

const connector = TestUtils.findRenderedComponentWithType(tree, Container);
expect(spy.calls.length).toBe(0);
connector.componentWillUnmount();
ReactDOM.unmountComponentAtNode(div);
expect(spy.calls.length).toBe(1);
});

it('should not attempt to set state after unmounting', () => {
const store = createStore(stringBuilder);
let mapStateToPropsCalls = 0;

@connect(
() => ({ calls: ++mapStateToPropsCalls }),
dispatch => ({ dispatch })
)
class Container extends Component {
render() {
return <Passthrough {...this.props} />;
}
}

const div = document.createElement('div');
store.subscribe(() =>
ReactDOM.unmountComponentAtNode(div)
);
ReactDOM.render(
<ProviderMock store={store}>
<Container />
</ProviderMock>,
div
);

expect(mapStateToPropsCalls).toBe(2);
const spy = expect.spyOn(console, 'error');
store.dispatch({ type: 'APPEND', body: 'a'});
spy.destroy();
expect(spy.calls.length).toBe(0);
expect(mapStateToPropsCalls).toBe(2);
});

it('should shallowly compare the selected state to prevent unnecessary updates', () => {
const store = createStore(stringBuilder);
const spy = expect.createSpy(() => ({}));
Expand Down