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

In React, lists are commonly displayed using the map() method. Sometimes an array contains no items. For example, a student list may be empty because no students have been added yet.

When an array is empty, the map() method does not render any list items. We can check the array length and display a helpful message instead of leaving the page blank.

Remember: An empty array has a length of 0. Use array.length === 0 to check whether a list is empty.

2. Rendering an Empty Array

When we use map() with an empty array, React has no list items to render. The surrounding JSX can still appear on the page.

Example

function StudentList() {
    const students = [];

    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

No list items are displayed because the students array is empty.

The heading is displayed, but the map() method returns no list items because the array contains no students.

3. Checking Whether a List Is Empty

The length property tells us how many elements an array contains. If the length is 0, the array is empty.

Example

function CourseList() {
    const courses = [];

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

            {courses.length === 0 && (
                <p>No courses available.</p>
            )}
        </div>
    );
}

export default CourseList;

Expected Output

Available Courses

No courses available.

Explanation: The condition courses.length === 0 is true because the array contains no elements. Therefore, React displays the message.

4. How Empty List Handling Works

The following diagram explains how React checks an array before deciding what content to display.

Check the Array courses.length
↓
Is the Length 0? courses.length === 0
↓
Yes Display an empty-list message
No Render the list items
Important: Conditional rendering allows React to display different content depending on whether the array contains elements.

5. Handling Empty Lists Using If-Else

We can use an if-else statement to decide whether to display an empty message or a list of items. This approach is useful when the two situations need different JSX.

Example

function BookList() {
    const books = [];

    if (books.length === 0) {
        return <p>No books found.</p>;
    }

    return (
        <ul>
            {books.map((book) => (
                <li key={book.id}>
                    {book.title}
                </li>
            ))}
        </ul>
    );
}

export default BookList;

Expected Output

No books found.

Since the books array is empty, the component returns the message. The list is rendered only when books are available.

6. Handling Empty Lists Using the Ternary Operator

The ternary operator provides a short way to choose between two outputs. We can use it to display either an empty message or the list of available items.

Example

function TaskList() {
    const tasks = [];

    return (
        <div>
            <h2>My Tasks</h2>

            {tasks.length === 0 ? (
                <p>No tasks available.</p>
            ) : (
                <ul>
                    {tasks.map((task) => (
                        <li key={task.id}>
                            {task.name}
                        </li>
                    ))}
                </ul>
            )}
        </div>
    );
}

export default TaskList;

Expected Output

My Tasks

No tasks available.

Remember: The first part of the ternary operator is rendered when the condition is true. The second part is rendered when it is false.

7. Displaying Empty and Non-Empty Lists

The same component can handle both empty and non-empty arrays. When data is available, React displays the items. Otherwise, React displays a message.

Example: Product List

function ProductList() {
    const products = [
        { id: 1, name: "Keyboard" },
        { id: 2, name: "Mouse" }
    ];

    return (
        <div>
            <h2>Products</h2>

            {products.length === 0 ? (
                <p>No products available.</p>
            ) : (
                <ul>
                    {products.map((product) => (
                        <li key={product.id}>
                            {product.name}
                        </li>
                    ))}
                </ul>
            )}
        </div>
    );
}

export default ProductList;

Expected Output

Products

  • Keyboard
  • Mouse

The products array contains two objects, so the condition is false and React renders both products using map().

8. Common Mistakes

Mistake 1: Checking the Array Incorrectly

const courses = [];

if (courses.length) {
    console.log("No courses available.");
}

An empty array has a length of 0, which is falsy in a conditional expression. Therefore, this condition does not display the message for an empty array.

Correct Code

const courses = [];

if (courses.length === 0) {
    console.log("No courses available.");
}

Mistake 2: Using an Incorrect Empty Condition

const students = [];

{students.length > 0 && (
    <p>No students available.</p>
)}

The condition is true only when students are available. Therefore, this code does not show the empty message when the array is empty.

Correct Code

const students = [];

{students.length === 0 && (
    <p>No students available.</p>
)}
Important: Match the condition to the message you want to display. Use length === 0 for empty arrays and length > 0 for non-empty arrays.

9. Practice Tasks

Complete these tasks to practise handling empty lists in React.

Task 1: Create an empty array of student names and display "No students available."
Task 2: Create a component that displays "No courses available." when the courses array is empty.
Task 3: Use an if-else statement to display an empty message or a list of books.
Task 4: Use the ternary operator to display either a task list or "No tasks available."
Task 5: Create a product list that displays product names when products are available and an appropriate message when the array is empty.

Summary

Handling empty lists in React helps provide clear feedback when an array contains no elements. The length property can check whether an array is empty, and conditional rendering can display an appropriate message or render available items using map(). We can use logical AND, if-else statements, or the ternary operator to handle empty and non-empty lists. These techniques make list components easier to understand and more useful.