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

Ternary Operator in React

Learn how to use the ternary operator to display different content based on conditions, with simple React examples from CIIT Training Institute.

1. What Is the Ternary Operator?

The ternary operator is a short way to choose between two values based on a condition. In React, it is commonly used inside JSX to display different messages or elements.

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: The ternary operator checks a condition. If it is true, the first value is selected. If it is false, the second value is selected.

2. Ternary Operator Syntax

The syntax of the ternary operator is:

condition ? valueIfTrue : valueIfFalse
TernarySyntax.jsx JavaScript
const age = 20;

const message = age >= 18
    ? "You are eligible."
    : "You are not eligible.";

console.log(message);

Code Explanation

age >= 18

Checks whether the age is at least 18.

?

Separates the condition from the true value.

"You are eligible."

This value is selected when the condition is true.

:

Separates the true value from the false value.

Expected Output

Console Output
You are eligible.

3. Ternary Operator Flow Diagram

The condition decides which result is selected. Only one of the two values is used.

CIIT TRAINING INSTITUTE — TERNARY OPERATOR
Check Condition isRegistered === true?
↓
TRUE (?)
↓
First Value Welcome to CIIT!
FALSE (:)
↓
Second Value Please register first.
Diagram Explanation: A true condition selects the value before the colon. A false condition selects the value after the colon.

4. First Ternary Operator Example in React

Let's display a message based on whether Samadhan is registered for a course at CIIT Training Institute.

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

    return (
        <div>
            {isRegistered
                ? <h2>Welcome, Samadhan!</h2>
                : <h2>Please register for a course.</h2>}
        </div>
    );
}

export default StudentWelcome;

Simple Explanation

const isRegistered = true;

Stores the student's registration status.

isRegistered ? ... : ...

Chooses which heading should be displayed.

<h2>Welcome, Samadhan!</h2>

Appears when the student is registered.

<h2>Please register for a course.</h2>

Appears when the student is not registered.

Expected Browser Output

Example Output

Welcome, Samadhan!

5. Interactive Example: Student Registration

Select a student and change the registration status. The message will change according to the condition.

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

    return (
        <div>
            {isRegistered
                ? <p>Welcome, {studentName}!</p>
                : <p>Please register, {studentName}.</p>}
        </div>
    );
}

export default RegistrationMessage;
Try It Yourself
Please register, Samadhan.

Expected Output

Example Output — Registered

Welcome, Samadhan!

6. Ternary Operator with Marks

The ternary operator can also select a result based on a student's marks. Here, marks of 40 or more mean Pass.

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

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

export default StudentResult;
Interactive Result Checker
Congratulations! You passed.
Simple Rule: If marks are greater than or equal to 40, the Pass message appears. Otherwise, the Fail message appears.

7. Ternary Operator with Course Availability

A ternary operator can select a message based on whether a course is available.

CourseAvailability.jsx JSX
function CourseAvailability() {
    const isAvailable = true;

    return (
        <div>
            {isAvailable
                ? <p>React course is available.</p>
                : <p>React course is currently unavailable.</p>}
        </div>
    );
}

export default CourseAvailability;

Expected Browser Output

Example Output

React course is available.

8. Common Mistakes

1. Forgetting the colon

Incorrect.jsx JSX
// Incorrect
isRegistered ? "Welcome" "Please register"
Correct Approach: Write a colon between the true and false values: isRegistered ? "Welcome" : "Please register".

2. Using a ternary operator for too many conditions

Nested ternary expressions can become difficult to read. For complex conditions, use if-else statements or another clear structure.

3. Using an if statement directly inside JSX braces

An ordinary if statement is a JavaScript statement, not an expression that can be placed directly inside JSX braces. Use a ternary expression or prepare the result before the return statement.

9. If-Else vs Ternary Operator

Feature If-Else Ternary Operator
Syntax if / else ? :
Usage Useful for longer logic. Useful for short two-way choices.
Inside JSX Use before the return statement. Can be used inside JSX braces.
Example Complex result checking. Showing a welcome message.

10. Practice Tasks

Task 1: Student Registration

Create a component for Samadhan that displays a welcome message if registered and a registration message otherwise.

Task 2: Student Result

Create a component for Pradnya that uses the ternary operator to display Pass or Fail based on marks.

Task 3: Course Availability

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

Task 4: Login Message

Create a component that displays "Welcome Back!" when the user is logged in and "Please Log In" otherwise.

Summary

  • The ternary operator is a short way to choose between two values.
  • Its syntax is condition ? trueValue : falseValue.
  • The question mark separates the condition from the true value.
  • The colon separates the true value from the false value.
  • It can be used inside JSX braces to display conditional content.
  • Use if-else when the logic becomes complex or difficult to read.