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

If-Else Rendering in React

Learn how to use if-else statements to display different content based on conditions in React, with simple examples from CIIT Training Institute.

1. What Is If-Else Rendering?

If-else rendering means displaying different UI content depending on whether a condition is true or false. React components can use normal JavaScript if-else statements to decide what JSX to return.

For example, CIIT Training Institute can display a welcome message when a student is registered and a registration message when the student is not registered.

Remember: If the condition is true, the if block runs. Otherwise, the else block runs.

2. Basic If-Else Syntax

JavaScript uses the if statement to check a condition and the else statement to handle the alternative case.

BasicIfElse.jsx JSX
if (condition) {
    // Code runs when condition is true
} else {
    // Code runs when condition is false
}

Simple Explanation

if (condition)

Checks whether the condition is true.

{ }

Contains the statements that should execute.

else

Runs when the if condition is false.

3. If-Else Flow Diagram

The condition decides which message or UI should be displayed. Only the matching branch is selected.

CIIT TRAINING INSTITUTE — CONDITIONAL RENDERING
Start React component executes
↓
Check Condition Is the student registered?
TRUE
↓
Welcome Message Welcome to CIIT!
FALSE
↓
Registration Message Please register first.
Diagram Explanation: A true condition selects the first branch. A false condition selects the second branch.

4. First React If-Else Example

Let's create a component that checks whether Samadhan is registered for a course at CIIT Training Institute.

StudentStatus.jsx JSX
function StudentStatus() {
    const isRegistered = true;

    if (isRegistered) {
        return (
            <h2>
                Welcome to CIIT Training Institute, Samadhan!
            </h2>
        );
    } else {
        return (
            <h2>
                Please register for a course.
            </h2>
        );
    }
}

export default StudentStatus;

Code Explanation

const isRegistered = true;

Stores the registration status. Here, the value is true.

if (isRegistered)

Checks whether Samadhan is registered.

return (...)

Returns the JSX that React should display.

else

Provides the alternative message when the condition is false.

Expected Browser Output

Example Output

Welcome to CIIT Training Institute, Samadhan!

Try the other condition: Change isRegistered = true to isRegistered = false. The registration message will be returned instead.

5. Interactive Example: Student Registration

Select a student and change the registration status to see how an if-else condition selects different messages.

RegistrationStatus.jsx JSX
function RegistrationStatus() {
    const studentName = "Pradnya";
    const isRegistered = false;

    if (isRegistered) {
        return <h3>Welcome, {studentName}!</h3>;
    } else {
        return <h3>Please register, {studentName}.</h3>;
    }
}

export default RegistrationStatus;
Try It Yourself
Please register for a course, Samadhan.

Expected Output

Example Output — Registered

Welcome to CIIT Training Institute, Samadhan!

What to observe: When the switch is on, the welcome message appears. When the switch is off, the registration message appears.

6. If-Else with Marks

We can also use if-else to display a student's result. In this example, a student passes when the marks are greater than or equal to 40.

StudentResult.jsx JSX
function StudentResult() {
    const marks = 75;

    if (marks >= 40) {
        return <h3>Congratulations! You passed.</h3>;
    } else {
        return <h3>You failed. Please try again.</h3>;
    }
}

export default StudentResult;

Interactive Result Checker

Try It Yourself
Congratulations! You passed.
Simple Rule: Marks of 40 or more mean Pass. Marks below 40 mean Fail. Values outside the range 0–100 are not accepted.

7. If-Else vs Ternary Operator

Both approaches can select content based on a condition. If-else is useful for longer logic, while the ternary operator is often convenient for short choices inside JSX.

Feature If-Else Ternary Operator
Syntax Uses if and else. Uses condition ? a : b.
Best for Longer or complex logic. Short two-way choices.
Inside JSX braces A statement cannot be placed directly there. A ternary expression can be used.
Readability Clear for multiple statements. Compact for simple conditions.
Remember: In a component, use if-else before the JSX return when you need to choose which block of JSX to return.

8. Common Mistakes

1. Forgetting the else case

IncorrectExample.jsx JSX
function Status() {
    const isReady = false;

    if (isReady) {
        return <h3>Ready</h3>;
    }

    return <h3>Not ready</h3>;
}

This example is valid too: the second return handles the false case without an explicit else block.

2. Putting an if statement directly inside JSX

Incorrect.jsx JSX
// Incorrect
return (
    <div>
        {if (isReady) { "Ready" }}
    </div>
);
Correct Approach: Use if-else before the return statement, or use a ternary expression inside JSX.

3. Using assignment instead of comparison

Comparison.jsx JavaScript
// Comparison
if (marks >= 40) {
    // Pass
} else {
    // Fail
}

Use a comparison such as === or >= when checking a condition.

9. Practice Tasks

Task 1: Student Registration

Create a component that checks whether Samadhan is registered for a React course.

  • Create an isRegistered variable.
  • Use if-else to check the value.
  • Display a welcome or registration message.

Task 2: Student Result

Create a result component for Pradnya.

  • Store marks in a variable.
  • Display Pass if marks are at least 40.
  • Display Fail otherwise.

Task 3: Course Availability

Create a component for Samiksha that displays whether a course is available.

  • Create a Boolean variable named isAvailable.
  • Use if-else to select the message.
  • Display the appropriate message for each condition.

Summary

  • If-else rendering selects UI based on a condition.
  • The if block runs when its condition is true.
  • The else block runs when the condition is false.
  • React components can return different JSX for different conditions.
  • Use if-else before the return statement for clear conditional logic.
  • Use the ternary operator for compact two-way expressions inside JSX.