React defining state without a constructor [duplicate]
问题 This question already has answers here : init state without constructor in react (3 answers) Closed 2 years ago . I've seen some react developers define state without a constructor. I like the fact that is simplifies the code, but is it safe to do so? class Dog extends React.Component { state = { sound: 'Woof' } return ( <p>Dog says { this.state.sound }</p> ) } I apologize in advance for the over simplified example. 回答1: Its exactly the same thing. Take a look at the javascript that babel