DEV Community

Class definition in React

Kayut on January 18, 2019

Does a Class component in React need to have a constructor like the following example: class App extends React.Component { constructor() { ...
Collapse
 
10secondsofcode profile image
Elango Sundar

@kayut If you don't have child class means, you can use as second option like without constructor.

Collapse
 
kozakrisz profile image
Krisztian Kecskes
Collapse
 
dance2die profile image
Sung M. Kim

👆 answers the question 👍

@kayut
And note that the state is initialized as a property outside of constructor, this. should be dropped.

class App extends React.Component {
  // not this.state but just state
  state = {
    count: 0
  };
  render() { ...  }
}