I've been reading up on decorators in JavaScript, and think I'm getting the basic premise.
Decorators are functions, they receive as a or several parameters what they should decorate, and return the result.
But I came over a @withStyles
decorated implementation in a React Boiler Plate project which I do not understand how works.
import React, { Component, PropTypes } from 'react';
function withStyles(...styles) {
return (BaseComponent) => class StyledComponent extends Component {
static contextTypes = {
insertCss: PropTypes.func.isRequired,
};
componentWillMount() {
this.removeCss = this.context.insertCss.apply(undefined, styles);
}
componentWillUnmount() {
this.removeCss();
}
render() {
return <BaseComponent {...this.props} />;
}
};
}
export default withStyles;
A use case would be
import s from './MyComponentStyle.scss';
@withStyles(s)
class MyComponent extends Component {
}
How does this work?