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

Embedding JavaScript Expressions in JSX

Learn how to display variables, calculations, function results, and dynamic values inside JSX using curly braces.

1. What Are JSX Expressions?

JSX allows you to embed JavaScript expressions inside markup by using curly braces { }. This makes it possible to display dynamic values instead of writing fixed text in your components.

An expression is JavaScript code that produces a value. For example, a variable, a mathematical calculation, or a function call that returns a value can be used inside JSX.

{ }

Important Rule

Use curly braces inside JSX to insert a JavaScript expression.

BasicExample.jsx JSX
function App() {
    const name = "Rahul";

    return <h1>Hello, {name}!</h1>;
}

export default App;

Expected Browser Output

Browser Output

Hello, Rahul!

2. Using Variables in JSX

Variables store data such as names, course names, numbers, and other values. You can display these values inside JSX using curly braces.

VariablesExample.jsx JSX
function App() {
    const studentName = "Priya";
    const course = "React JS";
    const city = "Pune";

    return (
        <div>
            <h1>Student Details</h1>
            <p>Name: {studentName}</p>
            <p>Course: {course}</p>
            <p>City: {city}</p>
        </div>
    );
}

export default App;

Expected Browser Output

Browser Output

Student Details

Name: Priya

Course: React JS

City: Pune

Remember: Write {studentName} to display the variable's value. Writing "studentName" displays the literal text instead.

3. Mathematical Expressions in JSX

You can perform mathematical calculations directly inside JSX. JavaScript evaluates the expression and displays the resulting value.

CalculationExample.jsx JSX
function App() {
    const a = 10;
    const b = 20;

    return (
        <div>
            <h2>Mathematical Operations</h2>
            <p>Addition: {a + b}</p>
            <p>Subtraction: {a - b}</p>
            <p>Multiplication: {a * b}</p>
            <p>Division: {a / b}</p>
            <p>Remainder: {a % 3}</p>
        </div>
    );
}

export default App;

Expected Browser Output

Browser Output

Mathematical Operations

Addition: 30

Subtraction: -10

Multiplication: 200

Division: 0.5

Remainder: 1

4. Using Function Calls in JSX

A JavaScript function that returns a value can be called inside JSX. The returned value is then displayed in the user interface.

FunctionExpression.jsx JSX
function getMessage() {
    return "Welcome to React!";
}

function App() {
    return (
        <div>
            <h1>{getMessage()}</h1>
        </div>
    );
}

export default App;

Expected Browser Output

Browser Output

Welcome to React!

Tip: Function calls inside JSX should produce the value you want to render. Avoid functions that perform side effects during rendering.

5. Expressions vs Statements

JSX curly braces accept JavaScript expressions, not arbitrary statements. An expression produces a value, whereas a statement performs an action or controls program execution.

JavaScript Code Type Can Be Used Directly in JSX?
name Variable expression Yes
10 + 20 Arithmetic expression Yes
getMessage() Function call expression Yes
isLoggedIn ? "Yes" : "No" Conditional expression Yes
if (isLoggedIn) { } Statement No
const total = 100; Variable declaration No

Correct Approach

CorrectExample.jsx JSX
function App() {
    const isLoggedIn = true;

    return (
        <div>
            <p>
                {isLoggedIn ? "Welcome Back!" : "Please Login"}
            </p>
        </div>
    );
}

export default App;
Note: Use a ternary expression for simple conditional output. For more complex logic, calculate the value before the return statement.

6. Boolean, Null and Undefined Values

Not every JavaScript value appears as visible text in JSX. Boolean values, null, and undefined are generally not rendered as text when used as children.

SpecialValues.jsx JSX
function App() {
    const isActive = true;
    const emptyValue = null;

    return (
        <div>
            <p>Boolean: {isActive}</p>
            <p>Empty value: {emptyValue}</p>
            <p>Number: {100}</p>
        </div>
    );
}

export default App;

Expected Browser Output

Browser Output

Boolean:

Empty value:

Number: 100

Important: To display a Boolean value as text, convert it to a string or use a conditional expression.
DisplayBoolean.jsx JSX
function App() {
    const isActive = true;

    return (
        <p>Active: {String(isActive)}</p>
    );
}

export default App;

Output: Active: true

7. Common Mistakes

1. Forgetting Curly Braces

Incorrect for a variable

<p>name</p>

Correct

<p>{name}</p>

Without curly braces, JSX displays the word name instead of the variable's value.

2. Using an if Statement Inside JSX

Incorrect

{if (isActive) { "Active" }}

Correct

{isActive ? "Active" : "Inactive"}

An if statement cannot be inserted directly into a JSX expression. Use a ternary expression or calculate the result before returning JSX.

3. Expecting Boolean Values to Display as Text

Not displayed as the text "true"

<p>{true}</p>

Correct

<p>{String(true)}</p>

Convert Boolean values to strings when you want to display the words true or false.

8. Practice Questions

Try solving these exercises in your React application before moving to the next topic.

01

Display Student Information

Create variables for a student's name, age, and course. Display all three values inside JSX.

02

Perform Calculations

Create two number variables and display their addition, subtraction, multiplication, and division results using JSX expressions.

03

Display a Dynamic Message

Create a Boolean variable called isLoggedIn. Display "Welcome Back!" when it is true and "Please Login" when it is false.

04

Calculate a Total

Create variables for price and quantity. Calculate the total amount and display it inside a paragraph using JSX.

9. Quick Recap

  • Use { } to embed JavaScript expressions inside JSX.
  • Variables can display dynamic data in a component.
  • Mathematical calculations can be evaluated inside JSX.
  • Function calls can display their returned values.
  • Expressions produce values; statements such as if cannot be inserted directly into JSX.
  • Boolean, null, and undefined values are not displayed as ordinary text by default.
Next Topic: JSX Attributes — learn how to pass static and dynamic values to HTML-like elements in JSX.