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 Rendering Lists

In React, lists are used to display multiple items such as courses, students, products, and employee names. Instead of writing separate JSX elements for every item, we can use the JavaScript map() method to create elements dynamically from an array.

The map() method visits each item in an array and returns a new array containing the result of a function applied to each item.

Remember: In React, map() is commonly used to transform array data into a collection of JSX elements.

2. Why Do We Use map()?

Suppose we want to display three course names.

Without map()

function CourseList() {
    return (
        <ul>
            <li>React</li>
            <li>Angular</li>
            <li>ASP.NET Core</li>
        </ul>
    );
}

This approach works, but writing every item separately becomes difficult when the array contains many items.

With map()

function CourseList() {
    const courses = ["React", "Angular", "ASP.NET Core"];

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

The map() method automatically creates one list item for each course in the array.

3. How Does map() Work?

The following diagram explains how an array is transformed into React list elements.

Array React, Angular, .NET
↓
map() Processes each item
↓
JSX Elements One li element per item
↓
Rendered List React · Angular · .NET
Important: React renders the returned JSX elements as a list. Each sibling list element should have a suitable key.

4. Syntax of map()

array.map((item) => {
    return (
        // JSX element
    );
});
  • array: The source array containing data.
  • item: The current item being processed.
  • map(): Creates a new array from the returned values.
  • return: Returns the value or JSX element for each item.

When an arrow function contains a single JSX expression, parentheses can be used for an implicit return.

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

5. Rendering a List of Names

In this example, the names array contains student names. The map() method creates a list item for every name.

Example

function StudentList() {
    const students = ["Amit", "Priya", "Rahul", "Sneha"];

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

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

export default StudentList;

Expected Output

Student Names

  • Amit
  • Priya
  • Rahul
  • Sneha

6. Rendering a List of Objects

Arrays can also contain objects. We can use map() to access object properties and display them in JSX.

Example

function CourseList() {
    const courses = [
        { id: 1, name: "React" },
        { id: 2, name: "Angular" },
        { id: 3, name: "ASP.NET Core" }
    ];

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

export default CourseList;

Expected Output

  • React
  • Angular
  • ASP.NET Core
Note: The object property course.name displays the course name, while course.id provides a stable key for each list item.

7. Why Are Keys Used with map()?

When rendering lists, React uses keys to identify items among their siblings. Keys help React match items when the list changes.

Example

{courses.map((course) => (
    <li key={course.id}>{course.name}</li>
))}
  • Use a unique and stable identifier whenever possible.
  • Keys should be unique among sibling elements.
  • Avoid generating a new random key on every render.
  • The key is used by React and is not available as a regular prop inside the child component.

The next lesson explains keys and unique keys in more detail.

8. Common Mistakes

Mistake 1: Forgetting to return JSX

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

With curly braces in an arrow function body, an explicit return is required. Otherwise, the callback returns undefined.

Correct Code

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

Mistake 2: Missing key

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

Add a suitable key to each sibling element returned from the map() callback.

Correct Code

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

9. Practice Tasks

Complete the following tasks to practise rendering lists with map().

Task 1: Create an array of five programming languages and display them in an unordered list using map().
Task 2: Create an array of student objects containing id and name. Display each student's name using map().
Task 3: Create an array of product objects containing id, name, and price. Display each product's name and price.
Task 4: Render a list of five CIIT training courses and assign a stable key to each list item.

Summary

The map() method is used in React to transform array data into JSX elements and render lists dynamically. It reduces repeated JSX code and makes it easier to display arrays of values or objects. Each item can be accessed through the callback parameter, and object properties can be displayed directly inside JSX. Every sibling element returned from a list should have a suitable key so React can identify items when the list changes.