State with Objects in React
Learn how to store student information in object state, update individual properties, preserve existing data, and build interactive student dashboards for CIIT Training Institute.
1. What Is an Object in React State?
An object is a JavaScript data structure that stores related information as key-value pairs. In a student dashboard, one object can store a student's name, course, email, and learning status.
Instead of creating a separate state variable for every property, we can group related information into one object state.
const student = {
name: "SAMADHAN",
course: "React Development",
status: "In Progress"
};
Object explanation:
namestores the student's name.coursestores the selected course.statusstores the current learning status.
2. Creating Object State with useState
We can initialize object state by passing an object to the useState Hook. React preserves that state between renders of the component.
import { useState } from "react";
function StudentProfile() {
const [student, setStudent] = useState({
name: "SAMIKSHA",
course: "Full Stack Development",
status: "In Progress"
});
return (
<div>
<h2>Student Profile</h2>
<p>Name: {student.name}</p>
<p>Course: {student.course}</p>
<p>Status: {student.status}</p>
</div>
);
}
export default StudentProfile;
Expected Browser Output
Student Profile
Name: SAMIKSHA
Course: Full Stack Development
Status: In Progress
The variable student contains the current
object value. The setter setStudent requests
updates to the entire object state.
3. Accessing Object Properties
Use dot notation to read an object's properties. For
example, student.name reads the name property,
while student.course reads the course property.
import { useState } from "react";
function StudentDetails() {
const [student, setStudent] = useState({
name: "PRADNYA",
course: "Software Testing",
completedLessons: 5
});
return (
<div>
<p>Name: {student.name}</p>
<p>Course: {student.course}</p>
<p>Completed Lessons: {student.completedLessons}</p>
</div>
);
}
export default StudentDetails;
Expected Browser Output
Name: PRADNYA
Course: Software Testing
Completed Lessons: 5
4. Updating One Property of an Object
A state setter replaces the state value with the value you pass. When updating one property of an object, spread the existing object first so that the other properties are preserved.
import { useState } from "react";
function StudentProfile() {
const [student, setStudent] = useState({
name: "SAMADHAN",
course: "React Development",
status: "In Progress"
});
function changeName() {
setStudent({
...student,
name: "SAMIKSHA"
});
}
return (
<div>
<p>Name: {student.name}</p>
<p>Course: {student.course}</p>
<p>Status: {student.status}</p>
<button onClick={changeName}>
Update Name
</button>
</div>
);
}
export default StudentProfile;
Explanation:
...studentcopies the existing properties.name: "SAMIKSHA"replaces the name property.- The course and status remain unchanged.
- A new object is passed to the setter.
Expected Browser Output
Before click: Name: SAMADHAN
After click: Name: SAMIKSHA
Course: React Development
Status: In Progress
5. Why Do We Use the Spread Operator?
The spread operator (...) copies the
enumerable properties of an object into a new object.
It helps us preserve existing properties while changing
only the property we need.
const student = {
name: "PRADNYA",
course: "Software Testing",
status: "In Progress"
};
const updatedStudent = {
...student,
status: "Completed"
};
Resulting Object
Name: PRADNYA
Course: Software Testing
Status: Completed
6. Updating Object State Using a Functional Update
If an object update depends on the previous state, use a functional updater. React supplies the previous object, allowing the new object to be calculated from the latest queued state value.
import { useState } from "react";
function StudentProfile() {
const [student, setStudent] = useState({
name: "SAMIKSHA",
completedLessons: 2
});
function completeLesson() {
setStudent(previousStudent => ({
...previousStudent,
completedLessons:
previousStudent.completedLessons + 1
}));
}
return (
<div>
<p>Student: {student.name}</p>
<p>Completed Lessons: {student.completedLessons}</p>
<button onClick={completeLesson}>
Complete Lesson
</button>
</div>
);
}
export default StudentProfile;
Expected Browser Output
Student: SAMIKSHA
Initially: Completed Lessons: 2
After one click: Completed Lessons: 3
This approach preserves the student's name and updates the completed lesson count using the previous object.
7. Updating Nested Object Properties
Objects can contain other objects. To update a nested property, create a new outer object and a new copy of the nested object that needs to change.
import { useState } from "react";
function StudentAddress() {
const [student, setStudent] = useState({
name: "PRADNYA",
address: {
city: "Pune",
state: "Maharashtra"
}
});
function updateCity() {
setStudent(previousStudent => ({
...previousStudent,
address: {
...previousStudent.address,
city: "Mumbai"
}
}));
}
return (
<div>
<p>Student: {student.name}</p>
<p>City: {student.address.city}</p>
<p>State: {student.address.state}</p>
<button onClick={updateCity}>
Update City
</button>
</div>
);
}
export default StudentAddress;
Expected Browser Output
Before click: City: Pune
After click: City: Mumbai
State: Maharashtra
8. Object State Update Flow
The following diagram explains how an object property can be updated without mutating the existing object.
9. Common Mistakes with Object State
Mistake 1: Mutating the existing object
student.name = "SAMIKSHA"; setStudent(student);
This mutates the existing object instead of creating a new object. React may not detect a meaningful change because the object reference remains the same.
Correct approach:
setStudent(previousStudent => ({
...previousStudent,
name: "SAMIKSHA"
}));
Mistake 2: Forgetting to preserve other properties
setStudent({
name: "PRADNYA"
});
This replaces the entire state object. Properties such as course and status are lost unless they are included in the new object.
Mistake 3: Forgetting to copy nested objects
Updating a nested property requires copying the nested object as well as the outer object.
10. Practice Exercises
Try these exercises in your React project:
- Create an object state for SAMADHAN with name, course, and status.
- Update SAMIKSHA's course status from In Progress to Completed.
- Update PRADNYA's completed lesson count using a functional updater.
- Change a student's city without changing the state property.
- Explain why the spread operator is useful for object state.
- Update one nested property while preserving the other properties.
11. Quick Revision
- Objects group related information into key-value pairs.
- useState can store an object as state.
- Use dot notation to read object properties.
- Use the spread operator to preserve existing properties.
- Use functional updates when the next object depends on previous state.
- Copy nested objects when updating nested properties.
- Never mutate an existing state object directly.
Summary
Object state allows React components to manage related information together, such as student names, course details, and learning progress. To update one property, create a new object using the spread operator and pass it to the setter. When the next object depends on previous state, use a functional updater. For nested objects, copy each object level that needs to change. These techniques help keep React state predictable and make CIIT Training Institute's student dashboard examples easier to maintain.