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

JSX Rules and Best Practices

Learn the essential JSX syntax rules, common mistakes, and best practices for writing clean and readable React code.

1. Introduction to JSX Rules

JSX allows developers to describe user interfaces using HTML-like syntax inside JavaScript. However, JSX follows specific syntax rules that must be followed to write valid React components.

Remember: Valid JSX requires properly closed tags, one parent element or Fragment for sibling elements, and correct attribute naming.
JSXRules.jsx JSX
function App() {
    return (
        <div>
            <h2>Welcome to CIIT Training Institute</h2>
            <p>Learn React step by step.</p>
        </div>
    );
}

export default App;

2. Return One Parent Element

A component must return one JSX element. When you need multiple sibling elements, wrap them in a parent element or a React Fragment.

Incorrect JSX
return (
    <h2>Student Details</h2>
    <p>Samadhan</p>
);

Multiple sibling elements are returned without a common parent.

Correct JSX
return (
    <div>
        <h2>Student Details</h2>
        <p>Samadhan</p>
    </div>
);

Both elements are inside one parent.

You can also use Fragment syntax <>...</> when an additional DOM wrapper is unnecessary.

3. Close Every JSX Tag

Every JSX element must have a closing tag or use self-closing syntax when appropriate. This also applies to elements such as images and input fields.

ClosingTags.jsx JSX
function App() {
    return (
        <div>
            <h2>Student Profile</h2>
            <p>Student Name: Samiksha</p>

            <input type="text" placeholder="Enter name" />

            <img src="/images/student.png"
                 alt="Student profile" />
        </div>
    );
}

export default App;
Illustrative Browser Output

Student Profile

Student Name: Samiksha

The example image path is illustrative. Add the image to your React application's assets when implementing this example.

4. Use className Instead of class

JSX uses className to assign CSS classes to elements. This follows JavaScript DOM property naming conventions.

Avoid in JSX <h2 class="student-title">Samadhan</h2>
Correct JSX <h2 className="student-title">Samadhan</h2>
ClassNameExample.jsx JSX
function App() {
    return (
        <div className="student-card">
            <h2 className="student-title">
                Samadhan
            </h2>

            <p className="student-description">
                Learning React at CIIT Training Institute.
            </p>
        </div>
    );
}

export default App;

5. Use htmlFor for Labels

In JSX, use htmlFor instead of the HTML for attribute when associating a label with a form control.

LabelExample.jsx JSX
function App() {
    return (
        <div>
            <label htmlFor="studentName">
                Student Name
            </label>

            <input
                id="studentName"
                type="text"
                placeholder="Enter student name"
            />
        </div>
    );
}

export default App;

Expected Browser Output

Illustrative Browser Output

6. Use Curly Braces for Expressions

JavaScript variables and expressions can be inserted into JSX using curly braces. Use quotation marks for literal strings and curly braces for JavaScript values.

ExpressionsExample.jsx JSX
function App() {
    const studentName = "Pradnya";
    const completedLessons = 8;
    const totalLessons = 12;

    return (
        <div>
            <h2>Student Progress</h2>
            <p>Name: {studentName}</p>
            <p>Completed: {completedLessons}</p>
            <p>Remaining: {totalLessons - completedLessons}</p>
        </div>
    );
}

export default App;
Illustrative Browser Output

Student Progress

Name: Pradnya

Completed: 8

Remaining: 4

7. Use Correct Event Names

JSX event handler names commonly use camelCase, such as onClick. Pass a function to the event attribute instead of calling it immediately.

EventExample.jsx JSX
function App() {
    function handleClick() {
        alert("Welcome, Samiksha!");
    }

    return (
        <button onClick={handleClick}>
            Show Welcome Message
        </button>
    );
}

export default App;
Incorrect onclick={handleClick} onClick={handleClick()}
Correct onClick={handleClick}

Pass the function reference to the event handler.

8. Use Keys When Rendering Lists

When rendering arrays of elements, provide a stable and unique key among sibling items. Keys help React identify items when a list changes.

ListKeys.jsx JSX
function App() {
    const students = [
        { id: 1, name: "Samadhan" },
        { id: 2, name: "Samiksha" },
        { id: 3, name: "Pradnya" }
    ];

    return (
        <div>
            <h2>Student List</h2>

            <ul>
                {students.map((student) => (
                    <li key={student.id}>
                        {student.name}
                    </li>
                ))}
            </ul>
        </div>
    );
}

export default App;

Expected Browser Output

Illustrative Browser Output

Student List

  • Samadhan
  • Samiksha
  • Pradnya
Avoid generating random keys during every render. Use stable identifiers from your data whenever possible.

9. Follow JSX Style Rules

The JSX style attribute expects a JavaScript object. CSS property names are generally written in camelCase, and string values are used for colors.

StyleExample.jsx JSX
function App() {
    const headingStyle = {
        color: "blue",
        backgroundColor: "lightyellow",
        padding: "12px",
        borderRadius: "8px"
    };

    return (
        <h2 style={headingStyle}>
            Welcome, Samadhan!
        </h2>
    );
}

export default App;
Remember: Use style={headingStyle} to pass a JavaScript object. Do not pass a plain CSS string to the JSX style attribute.

10. Keep Components Clean and Readable

Write small, focused components, use meaningful variable names, and keep JSX indentation consistent. Readable code is easier to maintain, test, and debug.

ReadableComponent.jsx JSX
function StudentCard() {
    const studentName = "Pradnya";
    const courseName = "React JS";

    return (
        <div className="student-card">
            <h2>{studentName}</h2>
            <p>Course: {courseName}</p>
        </div>
    );
}

export default StudentCard;
Best practice: Choose names that clearly explain the purpose of a variable, function, or component.

11. Practice Questions

  1. Correct a component that returns multiple sibling elements without a parent.
  2. Fix missing closing tags in a JSX component.
  3. Create a student profile using className and htmlFor.
  4. Display Samiksha's course progress using JavaScript expressions.
  5. Render Samadhan, Samiksha, and Pradnya using map() and stable keys.
  6. Create a button with an onClick event handler.
  7. Apply inline styles through a JavaScript object.

Summary

JSX follows specific rules for returning elements, closing tags, naming attributes, and embedding JavaScript expressions. Use one parent element or a Fragment for sibling elements, className for CSS classes, htmlFor for labels, and camelCase for event handlers. Use stable keys when rendering lists, and write clean, readable components to make React applications easier to maintain.