【发布时间】:2018-01-10 03:40:11
【问题描述】:
我在某处看到了下面的代码,我很好奇。它看起来很干净,但对我来说很不寻常。为什么 state = {} 声明时没有构造函数?
和load 声明时没有函数关键字?据我所知,有很多方法可以编写函数
function async load() {}
或者
const async load = ()=>{}
...args 做了什么?是否传播arguments?
import View from './View';
import loadData from './loadData';
export default class extends Component {
state = {};
load = this.load.bind(this);
async load(...args) {
try {
this.setState({ loading: true, error: false });
const data = await loadData(...args);
this.setState({ loading: false, data });
} catch (ex) {
this.setState({ loading: false, error: true });
}
}
render() {
return (
<View {...this.props} {...this.state} onLoad={this.load} />
);
}
}
【问题讨论】:
标签: javascript reactjs ecmascript-6