【发布时间】:2017-02-18 19:02:29
【问题描述】:
是否建议在 React 组件中拥有其他对象?这样做有什么缺点吗?我看到它完成了here
这是我的例子:
import React, {Component} from 'react';
import Utility from './Utility';
export default class MyComponent extends Component {
static defaultProps = {
message: 'Hello',
name: 'John!'
};
constructor(props) {
super(props);
this.utility = new Utility();
}
render() {
return (
<h1>{this.props.message}, {this.props.name} {this.utility.getText()}</h1>
);
}
}
Utility 是为组件提供更多功能的类。我检查过的大多数示例都没有这种东西。如果可以用的话,是在构造函数中实例化还是在挂载函数中实例化更好?
【问题讨论】:
标签: javascript reactjs ecmascript-6 es6-class