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 94af7d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"dependencies": {
"hoist-non-react-statics": "^3.0.0",
"loader-utils": "^1.2.3",
"react": "^16.3.0"
"react": "^16.3.0",
"prop-types": "^15.7.2"
},
"devDependencies": {
"@babel/core": "^7.3.3",
Expand All @@ -53,7 +54,6 @@
"husky": "^1.3.1",
"jest": "^24.1.0",
"prettier": "^1.16.4",
"prop-types": "^15.7.2",
"react-dom": "^16.8.2",
"rollup": "^1.2.1",
"rollup-plugin-babel": "^4.3.2",
Expand Down
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.isRequired,
}
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 94af7d6

Please sign in to comment.