In the constructor is preferable when using ES6 classes, but don't use the setState API, rather do like so:export default class MyComponent extends React.Component{ constructor(props){ super(props); this.state = { key: value }; }}此外,如果您有可用的类属性(babel stage 1),那么您可以执行以下操作:Also, if you have class properties available to you (babel stage 1) then you can do the following:export default class MyComponent extends React.Component{ state = { key: value }; render() { .... }} 这篇关于在构造函数或 componentWillMount 中设置初始反应组件状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 08:27