Skip to content

Commit

Permalink
Use forwardRef to pass on the ref(kriasoft#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaychaudhary committed Apr 8, 2020
1 parent a990d94 commit 1048086
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/withStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import React from 'react'
import PropTypes from 'prop-types';
import hoistStatics from 'hoist-non-react-statics'

import StyleContext from './StyleContext'
Expand All @@ -27,17 +28,24 @@ function withStyles(...styles) {
}

render() {
return <ComposedComponent {...this.props} />
const {__$$withStylesRef, ...props} = this.props;
return <ComposedComponent ref={__$$withStylesRef} {...props} />
}
}

const displayName = ComposedComponent.displayName || ComposedComponent.name || 'Component'

WithStyles.displayName = `WithStyles(${displayName})`
WithStyles.propTypes = {
__$$withStylesRef: PropTypes.func,
}
WithStyles.contextType = StyleContext
WithStyles.ComposedComponent = ComposedComponent

return hoistStatics(WithStyles, ComposedComponent)
const ForwardedWithStyles = React.forwardRef((props, ref) => <WithStyles {...props} __$$withStylesRef={ref} />)

ForwardedWithStyles.ComposedComponent = ComposedComponent
ForwardedWithStyles.displayName = `WithStyles(${displayName})`

return hoistStatics(ForwardedWithStyles, ComposedComponent);
}
}

Expand Down

0 comments on commit 1048086

Please sign in to comment.