State initialization is the process of setting the starting (default) values of a component’s state. Proper initialization ensures that your component renders predictable and stable data before any updates occur.
In React, you can initialize state in:
useState() hookconstructor and this.state0 or "".props or calculations.useState so heavy work runs only once.this.state inside the constructor.Functional components (with useState):
const [state, setState] = useState(initialValue);
The initialValue is used only on the first render. Later updates ignore this value.
Class components:
Inside the constructor, you assign an object to this.state:
this.state = { key: initialValue };
You must call super() before setting this.state.
Functional components use the useState() hook for state initialization.
// Functional component that initializes count to 0
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h3>Count: {count}</h3>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
useState(0) initializes count with 0 on the first render.setCount updates the state and triggers a re-render.count.When the initial state value requires heavy computation, you can pass a function to useState. This function runs only once, during the initial render.
// Lazy state initialization with an expensive calculation
function ExpensiveComponent() {
const [result, setResult] = useState(() => {
console.log("Calculating initial state...");
return 100 * 2; // Runs only once
});
return <h3>Result: {result}</h3>;
}
useState is executed only on the first render.In class-based components, state is initialized inside the constructor using this.state.
// Class component with state initialized in the constructor
import React, { Component } from "react";
class Profile extends Component {
constructor() {
super();
this.state = {
name: "Guest",
age: 20,
};
}
render() {
return (
<div>
<h3>Name: {this.state.name}</h3>
<p>Age: {this.state.age}</p>
</div>
);
}
}
super() before using this in the constructor.this.state holds an object with initial values.Sometimes, initial state is derived from props — useful when a parent provides starting data.
// Functional component initializing state from incoming props
function Welcome({ initialName }) {
const [name, setName] = useState(initialName);
return (
<div>
<h3>Welcome, {name}!</h3>
<button onClick={() => setName("User Updated")}>Change Name</button>
</div>
);
}
name comes from props.initialName.setName updates the displayed name.You can initialize state with arrays or objects — React handles any JavaScript data type.
// Complex state with an object and nested array
function Student() {
const [info, setInfo] = useState({
name: "Riya",
marks: [90, 85, 88],
});
return (
<div>
<h4>Name: {info.name}</h4>
<p>Average: {info.marks.reduce((a, b) => a + b) / info.marks.length}</p>
</div>
);
}
name and marks.marks is an array; reduce is used to calculate the total.0 or "").Counter component that initializes its count to 10 using useState.initialValue).name and score.Goal: Learn different ways to initialize state correctly and efficiently using useState in functional components and this.state in class components.