Using constructor in class component reactjs – Code Example

React components can have state by setting this.state in their constructors. this.state should be considered as private to a React component that it’s defined in. In this article we are going to show a code example using constructor in class component in react js –

class Square extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: null,
    };
  }

  render() {
    return (
      <button className="square" onClick={() => console.log('click')}>
        {this.props.value}
      </button>
    );
  }
}

Source: Reactjs Docs