javascript - unnecessary constructor in react class - Stack Overflow

If you need to specify initial state in a class, I see people did thisclass App extends React.Component

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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745529996a4631649.html

相关推荐

  • javascript - unnecessary constructor in react class - Stack Overflow

    If you need to specify initial state in a class, I see people did thisclass App extends React.Component

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信