-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <WrappedComponent {...this.nextState} />; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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} />; | ||
} | ||
}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this is the case you're talking about? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(() => ({})); | ||
|
||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, thanks @gaearon