最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - unnecessary constructor in react class - Stack Overflow

programmeradmin1浏览0评论

If you need to specify initial state in a class, I see people did this

class App extends React.Component {
   constructor() { super(); this.state = { user: [] } }
   render() {
      return <p>Hi</p>
   }
}

but what's wrong without a constructor?

class App extends React.Component {
   state = { user: [] }
   render() {
      return <p>Hi</p>
   }
}

If you need to specify initial state in a class, I see people did this

class App extends React.Component {
   constructor() { super(); this.state = { user: [] } }
   render() {
      return <p>Hi</p>
   }
}

but what's wrong without a constructor?

class App extends React.Component {
   state = { user: [] }
   render() {
      return <p>Hi</p>
   }
}
Share Improve this question asked Dec 15, 2017 at 16:30 Jenny MokJenny Mok 2,80410 gold badges33 silver badges62 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

but what's wrong without a constructor?

There is nothing "wrong" with it. But it uses the class properties proposal which is not officially part of the language yet (since you tagged the question with ecmascript-6: It is not part of ES6). So you have to correctly configure your build system to be able to use it (in addition to what's needed for JSX).

发布评论

评论列表(0)

  1. 暂无评论