Skip to content

Commit 406ca62

Browse files
committed
Handle changes from dispatch inside componentDidMount (fixes #208)
1 parent 152c886 commit 406ca62

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/components/createConnector.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export default function createConnector(React) {
4444

4545
componentDidMount() {
4646
this.unsubscribe = this.context.store.subscribe(::this.handleChange);
47+
this.handleChange();
4748
}
4849

4950
componentWillReceiveProps(nextProps) {
@@ -59,7 +60,9 @@ export default function createConnector(React) {
5960

6061
handleChange(props = this.props) {
6162
const nextState = this.selectState(props, this.context);
62-
this.setState(nextState);
63+
if (!this.isSliceEqual(this.state.slice, nextState.slice)) {
64+
this.setState(nextState);
65+
}
6366
}
6467

6568
selectState(props, context) {

test/components/Connector.spec.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ describe('React', () => {
245245
}
246246

247247
render() {
248-
return (<div>{this.props.string}</div>);
248+
return <div>{this.props.string}</div>;
249249
}
250250
}
251251

@@ -261,5 +261,35 @@ describe('React', () => {
261261

262262
expect(() => renderToString(el)).toNotThrow();
263263
});
264+
265+
it('should handle dispatch inside componentDidMount', () => {
266+
const store = createStore(stringBuilder);
267+
268+
class TestComp extends Component {
269+
componentDidMount() {
270+
store.dispatch({
271+
type: 'APPEND',
272+
body: 'a'
273+
});
274+
}
275+
276+
render() {
277+
return <div>{this.props.string}</div>;
278+
}
279+
}
280+
281+
const tree = TestUtils.renderIntoDocument(
282+
<Provider store={store}>
283+
{() => (
284+
<Connector select={string => ({ string })}>
285+
{({ string }) => <TestComp string={string} />}
286+
</Connector>
287+
)}
288+
</Provider>
289+
);
290+
291+
const testComp = TestUtils.findRenderedComponentWithType(tree, TestComp);
292+
expect(testComp.props.string).toBe('a');
293+
});
264294
});
265295
});

0 commit comments

Comments
 (0)