Skip to content

Commit

Permalink
fix: react warn if we try to get ref of pure component
Browse files Browse the repository at this point in the history
  • Loading branch information
vovkasm committed Jun 24, 2017
1 parent 6449d80 commit ffadc1e
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/stylable.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ function getDisplayName (comp) {
return 'Component'
}

function isPureComponent (comp) {
const p = comp.prototype
return p.isPureReactComponent || !p.setState
}

export default function stylable (name) {
return function wrapWithComponent (WrappedComponent) {
const stylableDisplayName = `Stylable(${getDisplayName(WrappedComponent)})`
const pureComponent = isPureComponent(WrappedComponent)

class Stylable extends React.Component {
static displayName = stylableDisplayName
Expand Down Expand Up @@ -60,20 +66,23 @@ export default function stylable (name) {
this.setState({ childProps: this.node.getChildProps() })
}

render () {
return React.createElement(WrappedComponent, {
...this.state.childProps,
ref: this._setWrappedRef
})
}

_setWrappedRef = (el) => { this._wrapped = el }

getChildContext () {
return { styleNode: this.node }
}
}

if (pureComponent) {
Stylable.prototype.render = function render () {
return React.createElement(WrappedComponent, this.state.childProps)
}
} else {
Stylable.prototype.render = function render () {
return React.createElement(WrappedComponent, { ...this.state.childProps, ref: this._setWrappedRef })
}
}

const proto = WrappedComponent.prototype
if (typeof proto.setNativeProps === 'function') {
Stylable.prototype.setNativeProps = function (props) {
Expand Down

0 comments on commit ffadc1e

Please sign in to comment.