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

Lists and Keys in React

Learn how to display multiple items in React and understand why keys are important when rendering lists.

1. What Are Lists in React?

A list is a collection of related items. In React, lists are commonly used to display multiple courses, students, products, employees, or other records in a component.

Instead of writing separate JSX elements for every item, we can store the data in an array and use JavaScript's map() method to create React elements from it.

Remember: React uses JSX to describe the UI, and map() helps create a JSX element for each item in an array.

2. Why Do We Use Lists?

Consider a training institute that offers several courses. We want to display all the course names on one page. Lists help us manage and display these items without repeating the same JSX structure.

CourseList.jsx JSX
function CourseList() {
    const courses = [
        "React",
        "ASP.NET Core MVC",
        "SQL Server",
        "Software Testing"
    ];

    return (
        <div>
            <h2>CIIT Training Institute Courses</h2>
            <ul>
                {courses.map((course) => (
                    <li key={course}>{course}</li>
                ))}
            </ul>
        </div>
    );
}

export default CourseList;

Code Explanation

  • The courses array stores the course names.
  • The map() method processes each course.
  • Each course is displayed inside an HTML list item using JSX.
  • The key prop helps React identify each item in the rendered list.

Expected Output

Browser Output

CIIT Training Institute Courses

  • React
  • ASP.NET Core MVC
  • SQL Server
  • Software Testing

3. How Lists Work in React

The following diagram explains how an array is converted into a list of React elements.

Array of Courses
React SQL Server ASP.NET Core
↓
courses.map()

Processes each array item

↓
React Elements
<li>React</li> <li>SQL Server</li> <li>ASP.NET Core</li>
↓
Rendered List in the Browser
  • React
  • SQL Server
  • ASP.NET Core

4. What Is the map() Method?

The map() method creates a new array by calling a function for each item in an existing array. In React, that function commonly returns JSX elements.

Students.jsx JSX
function Students() {
    const students = ["Amit", "Priya", "Rahul"];

    const studentItems = students.map((student) => (
        <li key={student}>{student}</li>
    ));

    return (
        <div>
            <h2>Student Names</h2>
            <ul>{studentItems}</ul>
        </div>
    );
}

export default Students;

Expected Output

Browser Output

Student Names

  • Amit
  • Priya
  • Rahul
Important: The map() method returns a new array. It does not directly modify the original array.

5. What Are Keys in React?

A key is a special prop that helps React identify which item in a list has changed, been added, or been removed. Keys should be unique among the sibling elements in a list.

KeyExample.jsx JSX
function KeyExample() {
    const courses = [
        { id: 101, name: "React" },
        { id: 102, name: "SQL Server" },
        { id: 103, name: "ASP.NET Core MVC" }
    ];

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

export default KeyExample;

Code Explanation

  • Each course object has a unique id.
  • key={course.id} provides a stable identity for each list item.
  • React uses keys when reconciling list elements between renders.

Expected Output

Browser Output
  • React
  • SQL Server
  • ASP.NET Core MVC

6. Choosing the Right Key

Use a stable and unique identifier from your data whenever possible. A database ID or a unique record ID is usually a good choice.

Key Type Example Recommendation
Unique ID key={course.id} Preferred when IDs are stable and unique.
Unique string key={course.code} Suitable if the value is unique and stable.
Array index key={index} Avoid for lists that can be reordered, inserted into, or deleted from.
Remember: Keys are used by React and are not automatically passed to your component as a normal prop.

7. Common Mistakes to Avoid

1. Forgetting the key prop

Not recommended:

{courses.map((course) => (
    <li>{course.name}</li>
))}

Correct:

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

2. Using a value that is not unique

If two records use the same key, React cannot reliably distinguish them in the list. Use a unique identifier for every sibling item.

8. Practice Tasks

Complete these exercises using React:

  1. Create an array of five course names and display them using map().
  2. Create a list of student names and render each name inside an li element.
  3. Create an array of course objects with unique IDs and display the course names.
  4. Create a product list where every item uses its product ID as the key.
  5. Explain why stable, unique keys are important when list items are added, removed, or reordered.
Practice Tip: Whenever you use map() to render sibling JSX elements, check that every element has an appropriate key.

Summary

Lists in React allow multiple items to be displayed from an array. The map() method is commonly used to transform array items into JSX elements. Each rendered item should have a suitable key so React can identify it between renders. Stable and unique IDs are usually the best choice for keys. Understanding lists and keys helps developers create organized, maintainable React components.