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

Filtering lists means displaying only those items that satisfy a particular condition. In React, the JavaScript filter() method is commonly used to filter array data before rendering it.

For example, an application may contain a list of courses, but we may want to display only development courses. We can use filter() to select the required courses and map() to render them as JSX elements.

Remember: filter() selects matching items, while map() transforms those items into JSX elements for rendering.

2. Understanding the filter() Method

The filter() method creates a new array containing only the elements for which the supplied callback returns true. The original array is not modified by filter().

Syntax

const filteredArray = array.filter((item) => {
    return condition;
});
  • array: The original array of items.
  • item: The current item being checked.
  • condition: Determines whether the item should be included.
  • filteredArray: A new array containing matching items.

Simple JavaScript Example

const numbers = [10, 15, 20, 25, 30];

const filteredNumbers = numbers.filter(
    (number) => number > 20
);

console.log(filteredNumbers);

Expected Output

[25, 30]

Only the numbers greater than 20 are included in the filtered array.

3. How Filtering Lists Works

The diagram shows how filter() selects matching items before map() renders them.

Original Array 10, 15, 20, 25, 30
↓
filter() Select numbers greater than 20
↓
Filtered Array 25, 30
↓
map() Creates JSX elements for rendering
Important: Filtering and rendering are separate operations. You can chain filter() and map() to perform both operations in one expression.

4. Filtering a List of Strings

Suppose we have a list of programming courses and want to display only the courses that contain the word "React".

Example

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

    const filteredCourses = courses.filter(
        (course) => course.includes("React")
    );

    return (
        <div>
            <h2>React Courses</h2>

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

export default CourseList;

Expected Output

React Courses

  • React Basics
  • React Advanced

The includes() method checks whether a string contains the specified text. Only matching course names are included in filteredCourses.

5. Filtering a List of Objects

Arrays of objects can also be filtered using object properties. For example, we can display only students who have scored at least 70 marks.

Example

function StudentList() {
    const students = [
        { id: 1, name: "Amit", marks: 85 },
        { id: 2, name: "Priya", marks: 65 },
        { id: 3, name: "Rahul", marks: 78 },
        { id: 4, name: "Sneha", marks: 55 }
    ];

    const qualifiedStudents = students.filter(
        (student) => student.marks >= 70
    );

    return (
        <div>
            <h2>Qualified Students</h2>

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

export default StudentList;

Expected Output

Qualified Students

  • Amit - 85 marks
  • Rahul - 78 marks
Explanation: The filter() method checks each student's marks. Only students scoring 70 or more are included in the qualifiedStudents array. Their IDs are used as React keys.

6. Combining filter() and map()

We can chain filter() and map() to filter an array and render the matching items directly.

Example

function ProductList() {
    const products = [
        { id: 1, name: "Keyboard", price: 800 },
        { id: 2, name: "Mouse", price: 500 },
        { id: 3, name: "Monitor", price: 7000 },
        { id: 4, name: "Headphones", price: 1500 }
    ];

    return (
        <div>
            <h2>Products Above ₹1000</h2>

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

export default ProductList;

Expected Output

Products Above ₹1000

  • Monitor - ₹7000
  • Headphones - ₹1500

First, filter() selects products priced above ₹1000. Then, map() converts the selected products into list items.

7. Filtering with Different Conditions

The filter() method can use different conditions depending on the application's requirements.

Example: Filtering Active Employees

function EmployeeList() {
    const employees = [
        { id: 1, name: "Amit", active: true },
        { id: 2, name: "Priya", active: false },
        { id: 3, name: "Rahul", active: true }
    ];

    const activeEmployees = employees.filter(
        (employee) => employee.active
    );

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

export default EmployeeList;

Expected Output

  • Amit
  • Rahul

Only employees whose active property is true are displayed.

8. When No Items Match

If no items satisfy the filter condition, filter() returns an empty array. React renders no list items for an empty array.

Example

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

    const filteredCourses = courses.filter(
        (course) => course === "Python"
    );

    return (
        <div>
            <h2>Search Results</h2>

            {filteredCourses.length > 0 ? (
                <ul>
                    {filteredCourses.map((course) => (
                        <li key={course}>{course}</li>
                    ))}
                </ul>
            ) : (
                <p>No courses found.</p>
            )}
        </div>
    );
}

export default CourseList;

Expected Output

Search Results

No courses found.

Note: Checking the filtered array's length lets you display a useful message when no items match.

9. Common Mistakes

Mistake 1: Using map() Instead of filter()

const numbers = [10, 20, 30];

const result = numbers.map(
    (number) => number > 15
);

This creates an array of Boolean values rather than selecting the matching numbers.

Correct Code

const result = numbers.filter(
    (number) => number > 15
);

// Result: [20, 30]

Mistake 2: Forgetting the Return Value

const result = numbers.filter((number) => {
    number > 15;
});

When using curly braces in the callback body, return the condition explicitly. Otherwise, the callback returns undefined.

Correct Code

const result = numbers.filter((number) => {
    return number > 15;
});

10. Practice Tasks

Complete these tasks to practise filtering lists in React.

Task 1: Create an array of numbers and display only numbers greater than 50 using filter().
Task 2: Create an array of student objects and display students who scored 75 marks or more.
Task 3: Create an array of product objects and display products priced below ₹2000.
Task 4: Create an employee list and display only active employees.
Task 5: Filter a list of courses by name and display "No courses found." when no course matches.

Summary

The filter() method creates a new array containing only the items that satisfy a specified condition. In React, it can be used to filter arrays of strings, numbers, or objects before rendering them. Combining filter() with map() makes it easy to select matching data and display the corresponding JSX elements. When no items match, the filtered array is empty, so conditional rendering can be used to display a helpful message.