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

Prop Drilling and Its Problems

1. What Is Prop Drilling?

Prop drilling is a situation in React where data is passed from a parent component to a deeply nested child component through multiple intermediate components using props.

Sometimes intermediate components do not need the data themselves. They only pass it to another component. This can make the code harder to maintain as the component tree grows.

Remember: Props are a normal way to pass data between components. Prop drilling becomes a problem when many intermediate components pass data they do not actually use.

2. Understanding Prop Drilling

Suppose an application stores a student's name in the App component. A deeply nested Student component needs to display that name. The value must pass through several intermediate components.

App (studentName)
↓ Props
Dashboard
↓ Props
StudentSection
↓ Props
Student

The student name passes through components before reaching the component that needs it.

3. Example of Prop Drilling

In this example, studentName is passed through Dashboard and StudentSection before it reaches Student.

src/App.jsx
function Student({ studentName }) {
    return <h2>Student Name: {studentName}</h2>;
}

function StudentSection({ studentName }) {
    return (
        <div>
            <h3>Student Section</h3>
            <Student studentName={studentName} />
        </div>
    );
}

function Dashboard({ studentName }) {
    return (
        <div>
            <h2>Dashboard</h2>
            <StudentSection studentName={studentName} />
        </div>
    );
}

function App() {
    const studentName = "Samadhan";

    return (
        <div>
            <h1>CIIT Training Institute</h1>
            <Dashboard studentName={studentName} />
        </div>
    );
}

export default App;
Explanation: The Student component needs the student's name, but Dashboard and StudentSection must also receive and forward it. This is the basic prop drilling pattern.

4. Problems with Prop Drilling

  • Intermediate components receive props they do not use.
  • Adding or changing data can require updates across several components.
  • Component interfaces can become unnecessarily complicated.
  • Code can become harder to read and maintain.
  • Large component trees can make shared data flow difficult to follow.

5. Solving Prop Drilling with Context API

React Context lets a parent provide a value to components further down the component tree without passing that value through every intermediate component.

We create a context, wrap the relevant components with its provider and access the value using useContext.

src/StudentContext.jsx
import { createContext } from "react";

const StudentContext = createContext(null);

export default StudentContext;
src/App.jsx
import StudentContext from "./StudentContext";
import Dashboard from "./Dashboard";

function App() {
    const studentName = "Samadhan";

    return (
        <StudentContext.Provider value={studentName}>
            <h1>CIIT Training Institute</h1>
            <Dashboard />
        </StudentContext.Provider>
    );
}

export default App;
src/Dashboard.jsx
import StudentSection from "./StudentSection";

function Dashboard() {
    return (
        <div>
            <h2>Dashboard</h2>
            <StudentSection />
        </div>
    );
}

export default Dashboard;
src/StudentSection.jsx
import Student from "./Student";

function StudentSection() {
    return (
        <div>
            <h3>Student Section</h3>
            <Student />
        </div>
    );
}

export default StudentSection;
src/Student.jsx
import { useContext } from "react";
import StudentContext from "./StudentContext";

function Student() {
    const studentName = useContext(StudentContext);

    return (
        <h2>Student Name: {studentName}</h2>
    );
}

export default Student;
Notice the difference: Dashboard and StudentSection no longer receive or forward studentName. The Student component reads the value directly from Context.

6. Props vs Context API

Props Context API
Passes data directly from parent to child. Provides values to components within a provider.
Works well for direct parent-child relationships. Useful for values needed by deeply nested components.
Data flow is explicit in component props. Consumers access values through Context.
Often ideal for component-specific data. Often useful for shared values such as themes or user preferences.

7. When Should You Use Context?

  • When many components need the same theme or language setting.
  • When deeply nested components need shared information.
  • When passing the same prop through many intermediate components becomes cumbersome.
Best practice: Do not replace every prop with Context. For simple parent-child communication, props are usually the clearest solution. Use Context when sharing a value genuinely simplifies the component tree.

8. Practice Tasks

  1. Create App, Dashboard, StudentSection and Student components.
  2. Pass a student's name through all intermediate components using props.
  3. Observe how each component must forward the prop.
  4. Refactor the example using Context API and useContext.
  5. Create a theme value shared by a Navbar and Footer.
  6. Explain when props are preferable to Context.

Summary

Prop drilling occurs when data passes through several intermediate React components before reaching the component that needs it. This can make larger component trees harder to maintain. React Context can reduce unnecessary prop forwarding by allowing descendant components to access shared values directly, while ordinary props remain useful for straightforward component communication.