Skip to content

Instantly share code, notes, and snippets.

@chengjianhua
Created March 8, 2017 03:52
Show Gist options
  • Save chengjianhua/5dbb0b6c1519ea322c249d4283a18660 to your computer and use it in GitHub Desktop.
Save chengjianhua/5dbb0b6c1519ea322c249d4283a18660 to your computer and use it in GitHub Desktop.
withStyle HOC for style-loader
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