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

1. Introduction to Lists of Objects

In React, an array can contain objects instead of simple values. Each object can store multiple properties, such as an ID, name, price, or category.

We can use the JavaScript map() method to iterate through an array of objects and display their properties inside JSX elements.

Remember: Use the map() method to iterate through the array, access object properties with dot notation, and provide a suitable key for each sibling element.

2. Understanding an Array of Objects

Consider the following array of student objects. Every object contains an ID and a name.

const students = [
    { id: 1, name: "Amit" },
    { id: 2, name: "Priya" },
    { id: 3, name: "Rahul" }
];

The students variable contains three objects. We can access their properties using expressions such as students[0].name and students[1].name.

Example Properties

ID Name
1 Amit
2 Priya
3 Rahul

3. How Rendering Lists of Objects Works

The following diagram explains how React transforms an array of objects into rendered list elements.

Array of Objects Student ID and Name
↓
map() Processes each student object
↓
Access Properties student.id and student.name
↓
Rendered JSX Student names displayed in a list

4. Rendering a List of Students

In this example, map() processes every student object and displays the student's name in an unordered list.

Example

function StudentList() {
    const students = [
        { id: 1, name: "Amit" },
        { id: 2, name: "Priya" },
        { id: 3, name: "Rahul" }
    ];

    return (
        <div>
            <h2>Student List</h2>

            <ul>
                {students.map((student) => (
                    <li key={student.id}>
                        {student.name}
                    </li>
                ))}
            </ul>
        </div>
    );
}

export default StudentList;

Expected Output

Student List

  • Amit
  • Priya
  • Rahul
Explanation: student.name displays each student's name, while student.id provides the key for that list item.

5. Displaying Multiple Object Properties

We can display more than one property from an object. For example, we can show a student's ID, name, and course.

Example

function StudentDetails() {
    const students = [
        { id: 1, name: "Amit", course: "React" },
        { id: 2, name: "Priya", course: "Angular" },
        { id: 3, name: "Rahul", course: "ASP.NET Core" }
    ];

    return (
        <div>
            <h2>Student Details</h2>

            {students.map((student) => (
                <div key={student.id}>
                    <p>ID: {student.id}</p>
                    <p>Name: {student.name}</p>
                    <p>Course: {student.course}</p>
                </div>
            ))}
        </div>
    );
}

export default StudentDetails;

Expected Output

Student Details

ID: 1

Name: Amit

Course: React

ID: 2

Name: Priya

Course: Angular

ID: 3

Name: Rahul

Course: ASP.NET Core

6. Example: Rendering Product Objects

The same approach can be used to display products. Each product object contains an ID, name, and price.

Example

function ProductList() {
    const products = [
        { id: 101, name: "Keyboard", price: 800 },
        { id: 102, name: "Mouse", price: 500 },
        { id: 103, name: "Monitor", price: 7000 }
    ];

    return (
        <div>
            <h2>Product List</h2>

            <ul>
                {products.map((product) => (
                    <li key={product.id}>
                        {product.name} - ₹{product.price}
                    </li>
                ))}
            </ul>
        </div>
    );
}

export default ProductList;

Expected Output

Product List

  • Keyboard - ₹800
  • Mouse - ₹500
  • Monitor - ₹7000

7. Using Destructuring with map()

JavaScript destructuring lets us extract object properties directly into variables. This can make list-rendering code shorter and easier to read.

Using Dot Notation

{students.map((student) => (
    <li key={student.id}>
        {student.name}
    </li>
))}

Using Destructuring

{students.map(({ id, name }) => (
    <li key={id}>
        {name}
    </li>
))}

Both examples render the student names. In the second example, id and name are extracted from each object directly in the callback parameter.

8. Common Mistakes

Mistake 1: Displaying the Entire Object

{students.map((student) => (
    <p key={student.id}>{student}</p>
))}

A plain JavaScript object cannot be rendered directly as a React child. Access a specific property such as student.name instead.

Mistake 2: Forgetting the Key

{students.map((student) => (
    <li>{student.name}</li>
))}

Provide a suitable key for every sibling element returned from the map() callback.

Mistake 3: Using a Property That Does Not Exist

{students.map((student) => (
    <li key={student.id}>
        {student.fullName}
    </li>
))}

If the objects contain a name property instead of fullName, the displayed value will be undefined. Use the property name defined in the data.

9. Practice Tasks

Complete these tasks to practise rendering arrays of objects.

Task 1: Create an array of five student objects with id, name, and course properties. Display all three properties.
Task 2: Create an array of employee objects with id, name, and department. Render the employees using map().
Task 3: Create an array of product objects with id, name, and price. Display each product and its price.
Task 4: Rewrite a student list example using destructuring instead of dot notation.

Summary

Arrays of objects allow React applications to work with structured data containing multiple properties. The map() method can iterate through these arrays and return JSX elements for each object. Dot notation or destructuring can be used to access properties such as ID, name, course, and price. Each sibling element should have a suitable key, preferably a stable and unique ID from the data.