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 Unique Keys

When React renders a list using the map() method, each sibling element should have a key. These keys help React identify individual items when a list changes.

Keys must be unique among sibling elements. If multiple elements in the same list use the same key, React may not correctly match those items during updates.

Remember: Keys must be unique among siblings, but they do not have to be globally unique across the entire application.

2. Why Are Unique Keys Important?

Consider a list of students. If two students have the same key, React cannot reliably distinguish those sibling items by their keys.

Unique keys help React:

  • Identify individual items in a list.
  • Match items between previous and updated renders.
  • Handle inserted, removed, and reordered items more reliably.
  • Avoid duplicate-key warnings in the development console.

3. Understanding Unique Keys

The diagram shows the difference between duplicate keys and unique keys in the same list.

Duplicate Keys

Student A key = 1
Student B key = 1

Duplicate key

Unique Keys

Student A key = 1
Student B key = 2

Unique sibling keys

Important: The key values in the correct example are different for each sibling item.

4. Incorrect Example: Duplicate Keys

In this example, both student objects have the same ID. Therefore, using student.id as the key produces duplicate keys in the list.

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

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

What Is the Problem?

  • Amit and Priya both use key 1.
  • The keys are not unique among sibling list items.
  • React can display a duplicate-key warning in development.

5. Correct Example: Unique Keys

Assign each student a distinct ID and use that ID as the key.

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

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

export default StudentList;

Expected Output

Student List

  • Amit
  • Priya
  • Rahul

Each student has a distinct ID, so each sibling list item receives a unique key.

6. Example: Unique Keys for Courses

The same rule applies when rendering course objects. Each course should have a stable, unique identifier.

function CourseList() {
    const courses = [
        { id: 101, name: "React" },
        { id: 102, name: "Angular" },
        { id: 103, name: "ASP.NET Core" }
    ];

    return (
        <div>
            <h2>CIIT Training Courses</h2>

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

export default CourseList;

Expected Output

CIIT Training Courses

  • React
  • Angular
  • ASP.NET Core

7. How to Choose a Unique Key

A key should identify an item consistently between renders. A unique ID from the data is generally the preferred choice.

Key Choice Recommendation
Unique database ID Preferred when stable and available
Stable unique data ID Good choice
Array index Avoid for lists that can change order or size
Random value generated on each render Avoid
Duplicate ID within the same list Incorrect

8. Common Mistakes

Mistake 1: Reusing the Same Key

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

Every list item receives the same key. Use a distinct stable identifier for each student.

Mistake 2: Generating Random Keys

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

A new random key can be generated on each render, causing React to treat existing items as different elements. Prefer stable IDs.

Mistake 3: Assuming Keys Must Be Globally Unique

Keys only need to be unique among sibling elements. Separate lists can reuse the same key values without violating this rule.

9. Practice Tasks

Complete these tasks to practise unique keys in React.

Task 1: Create an array of five student objects. Assign every student a different ID and render the names using map().
Task 2: Create a list of CIIT courses with unique course IDs. Use the IDs as keys.
Task 3: Find and correct duplicate IDs in an array of employee objects.
Task 4: Explain why random keys and array indexes can be unsuitable for lists that change.

Summary

Unique keys help React identify individual items when rendering lists. Each key must be unique among sibling elements and should remain stable between renders. Unique IDs from the data are generally a good choice. Duplicate keys can cause development warnings and unreliable item matching, while random keys can make React recreate items unnecessarily. Keys do not need to be globally unique across the entire application.