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

Rules of Hooks

Learn the important rules for using React Hooks correctly in functional components.

CIIT Training Institute | React Tutorial

1. What Are the Rules of Hooks?

React Hooks are special functions such as useState, useEffect, useRef, and useReducer. They allow functional components to use React features.

To make Hooks work correctly, React requires us to follow specific rules. These rules help React identify which state and effects belong to each component.

Remember: Always call Hooks at the top level of a React function component or a custom Hook.

2. Rule One: Call Hooks at the Top Level

Do not call Hooks inside conditions, loops, or nested functions. Call them at the top level of your component so React can preserve the same Hook order during every render.

Incorrect Example

Incorrect.jsx
import { useState } from 'react';

function Student() {
    const [isStudent, setIsStudent] = useState(true);

    if (isStudent) {
        const [name, setName] = useState('Samadhan');
    }

    return <h2>Student Component</h2>;
}

export default Student;

Here, useState is called inside an if condition. This breaks the Rules of Hooks.

Correct Example

Correct.jsx
import { useState } from 'react';

function Student() {
    const [isStudent, setIsStudent] = useState(true);
    const [name, setName] = useState('Samadhan');

    return (
        <div>
            <h2>Student Details</h2>
            <p>Name: {name}</p>
            <p>Student: {isStudent ? 'Yes' : 'No'}</p>
        </div>
    );
}

export default Student;
Both Hooks are called at the top level, before the component returns its JSX. This keeps their order consistent.

3. Understanding Hook Order

React relies on the order in which Hooks are called. If that order changes between renders, React may associate state or effects with the wrong Hook.

Component Render
↓
useState
↓
useEffect
↓
Return JSX

Keep Hook calls in the same order for every render. You can place conditions inside a Hook's callback or around ordinary logic, but do not conditionally call the Hook itself.

4. Rule Two: Call Hooks Only from React Functions

Hooks should be called from React function components or custom Hooks. Do not call Hooks directly inside ordinary JavaScript functions.

Incorrect Example

Incorrect.jsx
import { useState } from 'react';

function getStudentName() {
    const [name, setName] = useState('Samiksha');
    return name;
}

export default function Student() {
    return <h2>Student Details</h2>;
}

The function getStudentName is an ordinary JavaScript function, not a React component or custom Hook. Therefore, it must not call useState.

Correct Example

Correct.jsx
import { useState } from 'react';

function Student() {
    const [name, setName] = useState('Samiksha');

    return (
        <div>
            <h2>Student Details</h2>
            <p>Name: {name}</p>
        </div>
    );
}

export default Student;
React component names conventionally start with a capital letter. Custom Hook names start with use, such as useStudent or useCounter.

5. Using Hooks Inside a Custom Hook

A custom Hook is a JavaScript function whose name starts with use. It can call other Hooks at its top level. Custom Hooks help you reuse stateful logic across components.

useStudentName.jsx
import { useState } from 'react';

function useStudentName() {
    const [name, setName] = useState('Samadhan');

    function changeName() {
        setName('Samiksha');
    }

    return { name, changeName };
}

export default useStudentName;
Student.jsx
import useStudentName from './useStudentName';

function Student() {
    const { name, changeName } = useStudentName();

    return (
        <div>
            <h2>Student Name: {name}</h2>
            <button onClick={changeName}>
                Change Name
            </button>
        </div>
    );
}

export default Student;

Expected Output

Before clicking: Student Name: Samadhan

After clicking: Student Name: Samiksha

The custom Hook manages the name state and returns the information and function needed by the component.

6. Important Points to Remember

  • Call Hooks only at the top level of React components or custom Hooks.
  • Never call Hooks inside loops, conditions, or nested functions.
  • Call Hooks from React function components or custom Hooks only.
  • Do not call Hooks inside ordinary JavaScript functions.
  • Start custom Hook names with the word use.
  • Keep Hook calls in the same order during every render.
  • Use a React Hooks linting rule to help detect violations.

7. Practice Tasks

Complete the following tasks to check your understanding.

  1. Create a functional component that uses useState at the top level.
  2. Identify the error in a component that calls useState inside an if condition.
  3. Create a component that uses both useState and useEffect correctly.
  4. Create a custom Hook named useCounter that returns a count and an increment function.
  5. Explain why Hooks should not be called inside loops or nested functions.

Summary

React Hooks let functional components use features such as state and effects. To use Hooks correctly, call them only at the top level of React function components or custom Hooks, never inside conditions, loops, or nested functions. Following these rules keeps Hook calls consistent between renders and helps prevent unexpected behavior in React applications.