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

Context API Introduction

Learn how to share data between React components without passing props through every intermediate component.

1. What Is the Context API?

The Context API is a React feature that allows components to share information without manually passing props through every level of the component tree.

For example, an application may need to share the logged-in user's information, the selected theme, or the language preference with multiple components. Context can help make this information available to components that need it.

Remember: Context helps avoid unnecessary prop drilling when multiple components need the same data.

2. What Is Prop Drilling?

Prop drilling happens when data is passed from a parent component to a deeply nested child through intermediate components that may not need the data themselves.

Example: Without Context API

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

    return <Parent studentName={studentName} />;
}

function Parent({ studentName }) {
    return <Child studentName={studentName} />;
}

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

export default App;

Here, Parent passes studentName to Child, even though the Parent only forwards the value.

App
studentName
↓ Props
Parent
Forwards prop
↓ Props
Child
Uses data
As an application grows, passing many props through several intermediate components can make code harder to maintain.

3. How Does Context API Help?

Context lets a parent provide a value to components below it in the component tree. A component can read that value without receiving it as a prop from every intermediate component.

App
↓
Context Provider
Shares studentName
↓
Parent
↓
Child
Reads Context

The intermediate Parent no longer needs to forward the student name. The Child can read the shared value from Context when it is connected to the appropriate provider.

4. Main Parts of Context API

createContext

Creates a Context object that represents the value you want to share between components.

Context Provider

Makes a Context value available to components in its descendant tree. In modern React, you can use the provider syntax supported by your React version.

useContext

Reads the value of a Context from a component. The component receives the value from the closest matching provider above it, or the Context default value if no provider is found.

In the upcoming lessons, we will study createContext, useContext, Context Provider, reading values, and updating Context values step by step.

5. Simple Real-Life Example

Imagine CIIT Training Institute has a student portal. The student's name may be needed by the dashboard, profile section, and course section.

Student Portal
↓
Shared Student Context
↓
Dashboard
Profile
Courses

Instead of passing the student's name through every intermediate component, Context can make it available to the dashboard, profile, and courses components.

6. When Should You Use Context API?

  • When several components need the same logged-in user information.
  • When multiple components need the selected theme.
  • When language preferences must be shared across a component tree.
  • When passing props through many intermediate components becomes difficult to maintain.
Context is not required for every value. If data is needed by only a nearby child, passing props may be simpler and easier to understand.

7. Practice Tasks

Complete these tasks to check your understanding.

  1. Explain the meaning of the React Context API in your own words.
  2. Create an example where a parent passes a student name to a child using props.
  3. Draw a diagram showing prop drilling through three components.
  4. Explain how Context can reduce the need to forward props through intermediate components.
  5. Write the names and purposes of createContext, Provider, and useContext.
  6. Give two real-world examples where Context may be useful.

Summary

The React Context API allows components to share information without passing props through every intermediate component. It is useful for shared values such as user information, themes, and language preferences. The main concepts are createContext, a Context Provider, and useContext. In the next lessons, we will learn how to create Context and use it in React components.