Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

react set initial state without constructor

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"],
	};
}
 
PREVIOUS NEXT
Tagged: #react #set #initial #state #constructor
ADD COMMENT
Topic
Name
5+8 =