From f4f2d1f38370aa3d7184548cc9d22918f03128df Mon Sep 17 00:00:00 2001 From: Bradley Spaulding Date: Thu, 8 Oct 2015 11:50:18 -0700 Subject: [PATCH 1/2] connect doesn't apply ref to stateless component --- src/components/createConnect.js | 6 ++++++ test/components/connect.spec.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/components/createConnect.js b/src/components/createConnect.js index 5a7b9de41..c3d152630 100644 --- a/src/components/createConnect.js +++ b/src/components/createConnect.js @@ -77,6 +77,8 @@ export default function createConnect(React) { } return function wrapWithConnect(WrappedComponent) { + let isStateless = !(WrappedComponent.prototype && WrappedComponent.prototype.isReactComponent) || !('prototype' in WrappedComponent); + class Connect extends Component { shouldComponentUpdate(nextProps, nextState) { @@ -199,6 +201,10 @@ export default function createConnect(React) { } render() { + if (isStateless) { + return ; + } + return ( diff --git a/test/components/connect.spec.js b/test/components/connect.spec.js index 25f6eb8a9..92ac078af 100644 --- a/test/components/connect.spec.js +++ b/test/components/connect.spec.js @@ -1117,6 +1117,21 @@ describe('React', () => { expect(decorated.refs.wrappedInstance.someInstanceMethod()).toBe(someData); }); + it('should not add refs to wrapped instance if stateless', () => { + const store = createStore(() => ({})); + const decorator = connect(state => state); + const Decorated = decorator(() =>
); + + const tree = TestUtils.renderIntoDocument( + + + + ); + + const decorated = TestUtils.findRenderedComponentWithType(tree, Decorated); + expect(Object.keys(decorated.refs)).toExclude('wrappedInstance'); + }); + it('should wrap impure components without supressing updates', () => { const store = createStore(() => ({})); From ba23c7ea77bccdc334619459689eac74468c7857 Mon Sep 17 00:00:00 2001 From: Bradley Spaulding Date: Thu, 8 Oct 2015 15:42:19 -0700 Subject: [PATCH 2/2] Added failing spec for ES3 style stateful module --- test/components/connect.spec.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/components/connect.spec.js b/test/components/connect.spec.js index 92ac078af..507280dea 100644 --- a/test/components/connect.spec.js +++ b/test/components/connect.spec.js @@ -1132,6 +1132,30 @@ describe('React', () => { expect(Object.keys(decorated.refs)).toExclude('wrappedInstance'); }); + it('should consider module pattern of non-Component-inheriting classes stateful', () => { + function MyComponent(initialProps) { + return { + state: { value: initialProps.initialValue }, + render: function render() { + return ; + } + }; + } + + const store = createStore(() => ({})); + const decorator = connect(state => state); + const Decorated = decorator(() => ); + + const tree = TestUtils.renderIntoDocument( + + + + ); + + const decorated = TestUtils.findRenderedComponentWithType(tree, Decorated); + expect(Object.keys(decorated.refs)).toInclude('wrappedInstance'); + }); + it('should wrap impure components without supressing updates', () => { const store = createStore(() => ({}));