9
私はJavaScriptでデコレータを読んできました。私は基本的な前提を得ていると思います。ES7のクラスデコレータ
デコレータは関数で、デコレートすべきものを1つまたは複数のパラメータとして受け取り、結果を返します。
しかし、React Boiler Plateプロジェクトでは、@withStyles
のデコレーションされた実装が公開されましたが、どのように動作するのか分かりません。
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;
ユースケースは、この作業を行う方法
import s from './MyComponentStyle.scss';
@withStyles(s)
class MyComponent extends Component {
}
でしょうか?