Does a Class component in React need to have a constructor like the following example:
class App extends React.Component { constructor() { super() this.state = { count: 0 } } render() { ... } }
Or now we can define a Class component without using a constructor, like the following example?:
class App extends React.Component { this.state = { count: 0 } render() { ... } }
Which one is correct?
Do we still need to write the constructor for a Class component?
Top comments (3)
@kayut If you don't have child class means, you can use as second option like without constructor.
dev.to/kozakrisz/react---es6-trick...
๐ answers the question ๐
@kayut
And note that the
state
is initialized as a property outside of constructor,this.
should be dropped.