Created
March 8, 2017 03:52
-
-
Save chengjianhua/5dbb0b6c1519ea322c249d4283a18660 to your computer and use it in GitHub Desktop.
withStyle HOC for style-loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import hoistStatics from 'hoist-non-react-statics'; | |
function withStyles(style) { | |
return function wrapWithStyles(ComposedComponent) { | |
class WithStyles extends Component { | |
componentWillMount() { | |
style.use(); | |
} | |
componentWillUnmount() { | |
style.unuse(); | |
} | |
render() { | |
return <ComposedComponent {...this.props} />; | |
} | |
} | |
const displayName = ComposedComponent.displayName || ComposedComponent.name || 'Component'; | |
WithStyles.displayName = `WithStyles(${displayName})`; | |
WithStyles.ComposedComponent = ComposedComponent; | |
return hoistStatics(WithStyles, ComposedComponent); | |
}; | |
} | |
export default withStyles; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment