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

Multiple Conditions in React

Learn how to handle multiple conditions in React using if-else-if statements, logical operators, and conditional JSX.

1. What Are Multiple Conditions?

Multiple conditions allow a React component to choose different outputs depending on several possible situations. For example, a student result may be Pass, Fail, or Distinction depending on the student's marks.

React uses JavaScript conditional statements and expressions to decide which JSX elements should be returned and displayed.

Remember: Use conditions when your component needs to display different content for different values or situations.

2. Using if-else-if Statements

The if-else-if statement checks conditions in order. The first condition that evaluates to true determines which block of code executes.

if (condition1) { ... }
else if (condition2) { ... }
else { ... }
StudentResult.jsx JSX
function StudentResult() {
    const marks = 85;
    let result;

    if (marks >= 90) {
        result = "Distinction";
    } else if (marks >= 40) {
        result = "Pass";
    } else {
        result = "Fail";
    }

    return (
        <div>
            <h2>Student Result</h2>
            <p>Marks: {marks}</p>
            <p>Result: {result}</p>
        </div>
    );
}

export default StudentResult;

Code Explanation

  • marks stores the student's marks.
  • The first condition checks whether marks are at least 90.
  • The second condition checks whether marks are at least 40.
  • If neither condition is true, the result is Fail.
  • The component displays the selected result using JSX.

Expected Output

Browser Output

Student Result

Marks: 85

Result: Pass

3. Flow Diagram of Multiple Conditions

The following diagram shows how React component logic can evaluate multiple conditions before choosing the output.

Start: Read Student Marks
↓
Are marks greater than or equal to 90?
Yes
↓
Display Distinction
No
↓
Are marks greater than or equal to 40?
↓
Yes
↓
Display Pass
No
↓
Display Fail

4. Using Multiple Conditions in JSX

The ternary operator can be nested when you need to choose between more than two possible outputs. However, deeply nested ternary expressions can become difficult to read.

CourseStatus.jsx JSX
function CourseStatus() {
    const progress = 75;

    return (
        <div>
            <h2>Course Progress</h2>

            <p>
                {progress === 100
                    ? "Course Completed"
                    : progress >= 50
                        ? "Course In Progress"
                        : "Course Not Started"}
            </p>
        </div>
    );
}

export default CourseStatus;

Expected Output

Browser Output

Course Progress

Course In Progress

Best Practice: For many conditions, prefer a clear if-else-if statement or a separate function instead of deeply nested ternary expressions.

5. Combining Conditions with Logical Operators

Logical operators allow a component to check more than one condition together.

AdmissionStatus.jsx JSX
function AdmissionStatus() {
    const marks = 80;
    const documentsSubmitted = true;

    const isEligible =
        marks >= 50 && documentsSubmitted;

    return (
        <div>
            <h2>Admission Status</h2>

            {isEligible
                ? <p>Eligible for Admission</p>
                : <p>Not Eligible for Admission</p>}
        </div>
    );
}

export default AdmissionStatus;

Code Explanation

  • && means both conditions must be true.
  • The marks must be at least 50.
  • The required documents must be submitted.
  • If both conditions are true, the admission message appears.

Expected Output

Browser Output

Admission Status

Eligible for Admission

6. Multiple Conditions Using Element Variables

Element variables allow us to prepare different JSX elements and select one using conditional statements.

AttendanceStatus.jsx JSX
function AttendanceStatus() {
    const attendance = 82;
    let status;

    if (attendance >= 90) {
        status = <h3>Excellent Attendance</h3>;
    } else if (attendance >= 75) {
        status = <h3>Good Attendance</h3>;
    } else {
        status = <h3>Attendance Needs Improvement</h3>;
    }

    return (
        <div>
            <h2>Attendance Report</h2>
            <p>Attendance: {attendance}%</p>
            {status}
        </div>
    );
}

export default AttendanceStatus;

Expected Output

Browser Output

Attendance Report

Attendance: 82%

Good Attendance

7. Common Mistakes to Avoid

1. Using conditions in the wrong order

Incorrect:

if (marks >= 40) {
    result = "Pass";
} else if (marks >= 90) {
    result = "Distinction";
}

A mark of 95 satisfies marks >= 40, so the first branch executes. The Distinction condition is never reached for those marks.

Correct:

if (marks >= 90) {
    result = "Distinction";
} else if (marks >= 40) {
    result = "Pass";
} else {
    result = "Fail";
}

2. Forgetting the final else

When all conditions are false, a variable may remain undefined if no default value or final else is provided. Add a suitable fallback when every case needs an output.

8. Practice Tasks

Complete the following exercises using React:

  1. Create a result component that displays Distinction, Pass, or Fail based on marks.
  2. Create a temperature component that displays Cold, Moderate, or Hot based on temperature.
  3. Create an admission component that checks marks and document submission.
  4. Create a course progress component that displays Not Started, In Progress, or Completed.
  5. Create an attendance component that displays different messages for different attendance percentages.
Practice Tip: Test boundary values such as 39, 40, 89, and 90 to confirm that your conditions work correctly.

Summary

Multiple conditions in React help components display different content based on values and situations. The if-else-if statement is useful when several conditions must be checked in order. Ternary operators can select between expressions, while logical operators combine conditions. Element variables can store different JSX elements and render the appropriate one. Checking conditions in the correct order and providing a suitable fallback makes conditional rendering easier to understand and maintain.