React defining state without a constructor

camloken picture camloken · Apr 21, 2017 · Viewed 7.4k times · Source

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.

Answer

Shaun Sweet picture Shaun Sweet · Apr 21, 2017

Its exactly the same thing. Take a look at the javascript that babel transpiles the code into. There is no difference.