我正在使用具有a rule that enforces stateless react components to be rewritten as a pure function的airbnb eslint设置。以下组件触发此规则,这意味着下面的组件将更好地编写为纯函数:
import React from 'react';
import { observer } from 'mobx-react';
import cssmodules from 'react-css-modules';
import styles from './index.css';
import Select from '../Select/';
import List from '../List/';
@cssmodules(styles)
@observer
export default class Analysis extends React.Component {
render() {
return (
<div styleName="wrapper">
<div styleName="column">
<Select store={this.props.store} />
</div>
<div styleName="column">
<List store={this.props.store} />
</div>
</div>
);
}
}
Analysis.propTypes = {
store: React.PropTypes.object.isRequired,
};
但是,当我将其重写为纯函数(见下文)时,我收到错误Leading decorators must be attached to a class declaration
:
import React from 'react';
import { observer } from 'mobx-react';
import cssmodules from 'react-css-modules';
import styles from './index.css';
import Select from '../Select/';
import List from '../List/';
@cssmodules(styles)
@observer
function Analysis(props) {
return (
<div styleName="wrapper">
<div styleName="column">
<Select store={props.store} />
</div>
<div styleName="column">
<List store={props.store} />
</div>
</div>
);
}
Analysis.propTypes = {
store: React.PropTypes.object.isRequired,
};
那么我可以将其编写为纯组件并仍附加装饰器吗?或者这是airbnb linting规则中的一个错误,是否不可能满足这两个规则?
答案 0 :(得分:7)
好的,问题是es7样式装饰器。 Desugaring他们解决了这个问题:
public class MyObject
{
public string Title { get; set; }
public ErrorLevel ErrorLevel { get; set; }
}
它并不漂亮,但确实有效,并且不会触发任何错误。