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

Conditional Rendering in React

Learn how to display different content based on conditions using simple React examples and interactive output.

1. What Is Conditional Rendering?

Conditional rendering means displaying different content depending on whether a condition is true or false.

For example, a website can display a welcome message when a user is logged in and a login message when the user is logged out.

Simple Example:
Welcome.jsx JSX
function Welcome() {
    const isLoggedIn = true;

    if (isLoggedIn) {
        return <h2>Welcome to CIIT!</h2>;
    }

    return <h2>Please log in.</h2>;
}

export default Welcome;

Explanation: If isLoggedIn is true, React displays "Welcome to CIIT!". If it is false, React displays "Please log in.".

Example Output
Welcome to CIIT!

The condition is true, so the welcome message appears.

2. Conditional Rendering Using if-else

We can use a normal JavaScript if-else statement to decide which JSX content a component should return.

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

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

export default Result;
Simple Explanation: The condition checks whether marks are greater than or equal to 40. If the condition is true, the passing message appears. Otherwise, the failing message appears.
Interactive Marks Example
You passed!

3. Conditional Rendering Using a Variable

We can store JSX in a variable and choose what to display before returning the component.

CourseMessage.jsx JSX
function CourseMessage() {
    const hasCourse = true;
    let message;

    if (hasCourse) {
        message = <h3>React Course Selected</h3>;
    } else {
        message = <h3>Please select a course</h3>;
    }

    return <div>{message}</div>;
}

export default CourseMessage;

Here, the message variable stores the JSX that should be displayed. React renders the variable inside curly braces.

Interactive Course Example
Please select a course.

4. Conditional Rendering with the Ternary Operator

The ternary operator is a shorter way to choose between two values based on a condition.

Its syntax is:

condition ? valueIfTrue : valueIfFalse
LoginMessage.jsx JSX
function LoginMessage() {
    const isLoggedIn = false;

    return (
        <div>
            {isLoggedIn
                ? <h3>Welcome Back!</h3>
                : <h3>Please Log In</h3>}
        </div>
    );
}

export default LoginMessage;
Simple Explanation: If the condition is true, the first value is used. Otherwise, the value after the colon is used.
Interactive Login Example
Please Log In

5. Conditional Rendering Using Logical AND (&&)

The logical AND operator (&&) is useful when you want to display something only if a condition is true.

Notification.jsx JSX
function Notification() {
    const hasMessage = true;

    return (
        <div>
            {hasMessage && <p>You have a new message!</p>}
        </div>
    );
}

export default Notification;
Simple Explanation: The message appears only when hasMessage is true. If the condition is false, the message is not rendered.
Interactive Notification Example
You have a new message!
Important: Be careful when using && with numbers. For example, count && ... can render 0 when count is zero. Use count > 0 && ... if you only want to display content when the count is positive.

6. Multiple Conditions

We can check more than one condition using if-else if-else. This is useful when different results are required for different values.

Grade.jsx JSX
function Grade() {
    const marks = 85;
    let grade;

    if (marks >= 90) {
        grade = "A";
    } else if (marks >= 75) {
        grade = "B";
    } else if (marks >= 40) {
        grade = "C";
    } else {
        grade = "Fail";
    }

    return <h3>Grade: {grade}</h3>;
}

export default Grade;
Interactive Grade Calculator
Grade: B
Grade Rules:
  • 90–100: Grade A
  • 75–89: Grade B
  • 40–74: Grade C
  • Below 40: Fail

7. Difference Between Conditional Rendering Methods

Method Best Used For Example
if-else Complex conditions and multiple statements. Checking a student's result.
Variable Storing JSX before returning it. Choosing a message.
Ternary ?: Choosing between two outputs. Login or logout message.
Logical && Showing content only when true. Displaying a notification.

8. Practice Questions

  1. What is conditional rendering in React?
  2. How can you use an if-else statement to render JSX?
  3. What is the ternary operator?
  4. How does the logical AND operator work in JSX?
  5. What is the difference between a ternary operator and logical AND?
  6. Create a component that displays a message based on login status.
  7. Create a result checker that displays Pass or Fail.
  8. Create a notification that appears only when a checkbox is selected.

Summary

Conditional rendering allows React to display different content based on conditions. Use if-else for complex logic, variables to store JSX, the ternary operator to choose between two outputs, and && to display content only when a condition is true.