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

Element Variables in React

Learn how to store JSX elements in variables and render them inside React components using simple examples from CIIT Training Institute.

1. What Are Element Variables?

In React, an element variable is a variable that stores a React element or JSX expression. We can create the element once and use that variable inside the component's return statement.

Element variables are useful when we want to prepare JSX separately and then display it in the user interface.

Remember: A variable can store JSX, such as const message = <h2>Welcome!</h2>;. We can then render that variable using {' {message} '} inside JSX.

2. Syntax of Element Variables

The basic syntax is:

const element = JSX;
{'return
{element}
;'}
ElementVariableSyntax.jsx JSX
function App() {
    const message = <h2>Welcome to CIIT!</h2>;

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

export default App;

Code Explanation

1. Create a variable

message stores the JSX heading element.

2. Return JSX

The component returns a div element.

3. Render the variable

{'{message}'} inserts the stored React element into the returned JSX.

Expected Output

Browser Output

Welcome to CIIT!

3. How Element Variables Work

A React component creates an element variable, returns JSX, and uses the variable wherever that element should appear.

1
Create a Variable

Store JSX in a variable.

→
2
Return JSX

Write the component's UI.

→
3
Use the Variable

Insert it using curly braces.

→
4
Rendered UI

The element appears in the browser.

4. Example: Display a Student Message

CIIT Training Institute can store a student message in a variable and display it inside a React component.

StudentMessage.jsx JSX
function StudentMessage() {
    const studentMessage = (
        <div>
            <h2>Hello, Student!</h2>
            <p>Welcome to CIIT Training Institute.</p>
        </div>
    );

    return (
        <section>
            {studentMessage}
        </section>
    );
}

export default StudentMessage;

Expected Output

Browser Output

Hello, Student!

Welcome to CIIT Training Institute.

Explanation: The studentMessage variable stores a JSX div containing a heading and a paragraph. Using {'{studentMessage}'} renders both elements.

5. Element Variables with Conditional Rendering

Element variables can also work with conditional rendering. We can assign different JSX elements to the same variable depending on a condition.

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

    let message;

    if (isRegistered) {
        message = <h2>Registration Successful!</h2>;
    } else {
        message = <h2>Please Register First.</h2>;
    }

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

export default StudentStatus;

Code Explanation

  • isRegistered represents the registration status.
  • message is declared to hold a JSX element.
  • The if-else statement assigns the appropriate JSX element to message.
  • {'{message}'} renders the selected element.

Expected Output

Browser Output

Registration Successful!

Try it: Change isRegistered to false. The output will become Please Register First.

6. Example: Element Variable with a List

We can also store a JSX element containing a list of courses in a variable.

CourseList.jsx JSX
function CourseList() {
    const courseList = (
        <ul>
            <li>React Development</li>
            <li>ASP.NET Core MVC</li>
            <li>Data Analytics</li>
        </ul>
    );

    return (
        <div>
            <h2>Our Courses</h2>
            {courseList}
        </div>
    );
}

export default CourseList;

Expected Output

Browser Output

Our Courses

  • React Development
  • ASP.NET Core MVC
  • Data Analytics

7. Element Variables vs Direct JSX

Element Variable Direct JSX
JSX is stored in a variable before rendering. JSX is written directly inside the return statement.
Useful when an element is prepared separately or selected conditionally. Useful for simple and straightforward UI.
const title = <h2>Welcome</h2>; {'return

Welcome

;'}

8. Common Mistakes to Avoid

1. Forgetting curly braces

Incorrect:

return (
    <div>
        message
    </div>
);

Correct:

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

Without curly braces, message is treated as text instead of a JavaScript variable expression.

2. Assigning a string instead of JSX
const message = "<h2>Welcome!</h2>";

This stores a string, not a React element. Rendering it displays the text rather than creating an HTML heading.

Correct:

const message = <h2>Welcome!</h2>;

9. Practice Tasks

Complete these tasks using React:

  1. Create a variable named welcomeMessage that stores a heading welcoming students.
  2. Create a courseInfo variable containing a heading and a paragraph about a React course.
  3. Create an admissionMessage element variable using an if-else statement.
  4. Store an unordered list of three CIIT courses in a variable and render it.
  5. Create an element variable that displays different messages based on a student's result.
Practice Tip: Write each example inside a React component and render the variable inside its return statement.

Summary

Element variables allow React developers to store JSX elements in variables and render them inside a component. A variable can store a heading, paragraph, list, or a complete JSX structure. Curly braces are used to insert the variable inside JSX. Element variables can also be assigned conditionally using if-else statements to display different elements based on a condition. They help organize JSX and make conditional rendering easier to understand.