Master React From Scratch

Clear, practical, and structured React lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to React
Introduction to React What Is React? Features of React Advantages of React Limitations of React Uses of React React vs JavaScript React Architecture
2. React Getting Started
Getting Started Introduction Prerequisites Installing Node.js and npm Creating a React Project with Vite React Project Structure Running React Application Your First React Application
3. JSX
JSX Introduction What Is JSX? JavaScript Expressions JSX Attributes Rendering JSX Elements JSX Children React Fragments JSX Rules and Best Practices JSX vs HTML
4. React Components
Components Introduction Functional Components Class Components Component Composition Reusable Components Component Tree Component Lifecycle Import and Export Components
5. React Props
Props Introduction What Are Props? Passing Props to Components Props Destructuring Default Props Values Children Props Typing Props with TypeScript Props vs State
6. React State
State Introduction What Is State? useState Hook Updating State Functional State Updates State with Objects State with Arrays State Immutability Lifting State Up
7. React Events
Events Introduction Handling Events Passing Arguments to Event Handlers Synthetic Events Form Events Keyboard and Mouse Events Event Propagation
8. Conditional Rendering
Conditional Rendering Introduction If-Else Rendering Ternary Operator Logical AND Operator Element Variables Preventing Rendering Multiple Conditions Conditional CSS Classes
9. Lists and Keys
Lists and Keys Introduction Rendering Lists with map() Understanding Keys Choosing Unique Keys Lists of Objects Filtering Lists Sorting Lists Empty Lists and Conditional Rendering
10. React Forms
Forms Introduction Handling Forms Controlled Components Uncontrolled Components Handling Input Fields Checkboxes and Radio Buttons Select and Textarea Form Validation Form Submission
11. React Hooks
Hooks Introduction useState Hook useEffect Hook Effect Dependencies and Cleanup useRef Hook useReducer Hook useMemo Hook useCallback Hook Custom Hooks Rules of Hooks
12. React Context API
Context API Introduction createContext useContext Context Provider Reading Context Updating Context Using Multiple Contexts Context API vs Props Context API Best Practices
13. React Routing
Routing Introduction Introduction to React Router Installing React Router BrowserRouter Setup Routes and Route Components Link and NavLink Programmatic Navigation URL Parameters and useParams Nested Routes Not Found Routes Protected Routes
14. API Integration
API Integration Introduction Fetch API Async/Await with API Calls Displaying API Data Loading and Error States Sending POST Requests API Error Handling Using Axios Custom Fetch Hook API Environment Variables
15. State Management
State Management Introduction Global State Prop Drilling Introduction to Redux Redux Core Concepts Redux Toolkit Store and Slices useSelector and useDispatch Async Operations with Redux Toolkit State Management Best Practices
16. React Performance
Performance Introduction Understanding Re-Renders Avoiding Unnecessary Re-Renders React.memo useMemo Optimization useCallback Optimization React Developer Tools and Profiler Lazy Loading Components Code Splitting Optimizing Large Lists Performance Best Practices

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.

StudentObject.jsx
const student = {
    name: "SAMADHAN",
    course: "React Development",
    status: "In Progress"
};

Object explanation:

  • name stores the student's name.
  • course stores the selected course.
  • status stores the current learning status.
Remember: When an object is stored in React state, treat it as read-only. Create a new object when updating its properties.

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.

StudentProfile.jsx
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.

ReadStudent.jsx
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.

UpdateStudentName.jsx
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:

  • ...student copies 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.

ObjectSpread.jsx
const student = {
    name: "PRADNYA",
    course: "Software Testing",
    status: "In Progress"
};

const updatedStudent = {
    ...student,
    status: "Completed"
};

Resulting Object

Name: PRADNYA

Course: Software Testing

Status: Completed

Important: The spread operator makes a shallow copy. If an object contains nested objects, nested updates may require copying those nested objects too.

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.

FunctionalObjectUpdate.jsx
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.

NestedObjectUpdate.jsx
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

Remember: Copy every object level along the path to the property you want to update. The spread operator does not deep-copy nested objects automatically.

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

Incorrect
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:

Correct
setStudent(previousStudent => ({
    ...previousStudent,
    name: "SAMIKSHA"
}));

Mistake 2: Forgetting to preserve other properties

Potentially Incorrect
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:

  1. Create an object state for SAMADHAN with name, course, and status.
  2. Update SAMIKSHA's course status from In Progress to Completed.
  3. Update PRADNYA's completed lesson count using a functional updater.
  4. Change a student's city without changing the state property.
  5. Explain why the spread operator is useful for object state.
  6. Update one nested property while preserving the other properties.
Practice Tip: Run these examples in your React project and check whether the unchanged object properties remain intact.

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.