class App extends React.Component {
constructor(props) {
// Required step: always call the parent class' constructor
super(props);
// Set the state directly. Use props if necessary.
this.state = {
counter: 0,
}
}
render() {
// whatever you like
}
}
import React, { Component } from "react";
class Counter extends Component {
// you can either initialize state inside constructor
constructor() {
super();
this.state = {
count: 1,
tags: ["tag1", "tag2", "tag3"],
};
}
// or initialize the state as class field declaration
state = {
count: 1,
tags: ["tag1", "tag2", "tag3"],
};
}