React defining state without constructor

I've seen some developers react to defining state without a constructor. I love the fact that it simplifies the code, but is it safe to do it?

class Dog extends React.Component {
   state = { sound: 'Woof' }
   return (
      <p>Dog says { this.state.sound }</p>
   )
}

      

I apologize in advance for the oversimplified simplified example.

+3


source to share


1 answer


It's exactly the same. Take a look at the javascript babel is translating the code. There is no difference.



+3


source







All Articles