Skip to content
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

connect doesn't apply ref to stateless component #143

Closed
Closed
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
6 changes: 6 additions & 0 deletions src/components/createConnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export default function createConnect(React) {
}

return function wrapWithConnect(WrappedComponent) {
let isStateless = !(WrappedComponent.prototype && WrappedComponent.prototype.isReactComponent) || !('prototype' in WrappedComponent);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is a major concern, but this method of detecting stateful components isn't compatible with React < 0.14. Stateful components created in React 0.13 will be flagged as stateless as well. We had a similar situation at https://github.com/gajus/react-css-modules/issues/40

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we won't get this in before going 0.14+ either way.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, thanks @gaearon


class Connect extends Component {

shouldComponentUpdate(nextProps, nextState) {
Expand Down Expand Up @@ -199,6 +201,10 @@ export default function createConnect(React) {
}

render() {
if (isStateless) {
return <WrappedComponent {...this.nextState} />;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably not be here. If we decide to go ahead with this, we should probably create render above and apply it to this class.

return (
<WrappedComponent ref='wrappedInstance'
{...this.nextState} />
Expand Down
39 changes: 39 additions & 0 deletions test/components/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,45 @@ 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(() => <div/>);

const tree = TestUtils.renderIntoDocument(
<ProviderMock store={store}>
<Decorated />
</ProviderMock>
);

const decorated = TestUtils.findRenderedComponentWithType(tree, Decorated);
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 <span className={this.state.value} />;
}
};
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is the case you're talking about?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, interestingly, this turns out to not be a false positive. At least, React thinks this is stateless, and shows the warning.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I got confused. This spec is bad, wrapping it in a function below.


const store = createStore(() => ({}));
const decorator = connect(state => state);
const Decorated = decorator(() => <MyComponent/>);

const tree = TestUtils.renderIntoDocument(
<ProviderMock store={store}>
<Decorated />
</ProviderMock>
);

const decorated = TestUtils.findRenderedComponentWithType(tree, Decorated);
expect(Object.keys(decorated.refs)).toInclude('wrappedInstance');
});

it('should wrap impure components without supressing updates', () => {
const store = createStore(() => ({}));

Expand Down