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

Reading Context in Components

Learn how React components read shared data from Context using the useContext Hook.

1. What Does Reading Context Mean?

Reading Context means accessing a value provided by a React Context inside a functional component. React provides the useContext Hook for this purpose.

For example, a student dashboard, profile component, and course component may all need the same student information. Instead of passing that information through every intermediate component, each component can read the shared Context value directly.

Remember: A component reads the value from the closest matching Provider above it in the component tree. If there is no matching Provider, it receives the Context's default value.

2. Reading a Simple Context Value

First, create a Context with a default value.

ThemeContext.jsx
import { createContext } from 'react';

const ThemeContext = createContext('light');

export default ThemeContext;

Now use the Context inside a functional component.

ThemeDisplay.jsx
import { useContext } from 'react';
import ThemeContext from './ThemeContext';

function ThemeDisplay() {
    const theme = useContext(ThemeContext);

    return (
        <div>
            <h2>Current Theme: {theme}</h2>
        </div>
    );
}

export default ThemeDisplay;

Expected Output Without a Provider

Current Theme: light

The component displays light because no matching Provider supplies another value.

3. Reading Context from a Provider

A Provider can supply a different value to the component. Let's provide the theme as dark.

App.jsx
import ThemeContext from './ThemeContext';
import ThemeDisplay from './ThemeDisplay';

function App() {
    return (
        <ThemeContext.Provider value="dark">
            <ThemeDisplay />
        </ThemeContext.Provider>
    );
}

export default App;

Expected Output

Current Theme: dark

The Provider value takes precedence over the default value for components inside that Provider.

4. Reading an Object from Context

Context can hold an object containing multiple related values. This is useful when a component needs several pieces of shared information.

StudentContext.jsx
import { createContext } from 'react';

const StudentContext = createContext({
    name: 'Guest Student',
    course: 'Not Selected',
    city: 'Not Available'
});

export default StudentContext;
StudentDetails.jsx
import { useContext } from 'react';
import StudentContext from './StudentContext';

function StudentDetails() {
    const student = useContext(StudentContext);

    return (
        <div>
            <h2>Student Details</h2>
            <p>Name: {student.name}</p>
            <p>Course: {student.course}</p>
            <p>City: {student.city}</p>
        </div>
    );
}

export default StudentDetails;
App.jsx
import StudentContext from './StudentContext';
import StudentDetails from './StudentDetails';

function App() {
    const student = {
        name: 'Samadhan',
        course: 'React Development',
        city: 'Pune'
    };

    return (
        <StudentContext.Provider value={student}>
            <StudentDetails />
        </StudentContext.Provider>
    );
}

export default App;

Expected Output

Student Details

Name: Samadhan

Course: React Development

City: Pune

5. Reading Context in Multiple Components

Multiple components can read the same Context independently when they are descendants of the same Provider.

StudentName.jsx
import { useContext } from 'react';
import StudentContext from './StudentContext';

function StudentName() {
    const student = useContext(StudentContext);

    return <h2>Name: {student.name}</h2>;
}

export default StudentName;
StudentCourse.jsx
import { useContext } from 'react';
import StudentContext from './StudentContext';

function StudentCourse() {
    const student = useContext(StudentContext);

    return <p>Course: {student.course}</p>;
}

export default StudentCourse;
App.jsx
import StudentContext from './StudentContext';
import StudentName from './StudentName';
import StudentCourse from './StudentCourse';

function App() {
    const student = {
        name: 'Samiksha',
        course: 'Data Analytics',
        city: 'Pune'
    };

    return (
        <StudentContext.Provider value={student}>
            <StudentName />
            <StudentCourse />
        </StudentContext.Provider>
    );
}

export default App;

Expected Output

Name: Samiksha

Course: Data Analytics

Both components read from the same Provider. Neither needs to receive the student object through props.

6. Understanding the Component Tree

App
↓
StudentContext.Provider
↓
StudentName
StudentCourse
↓
useContext(StudentContext)

The Provider makes the value available to its descendants. Each component that calls useContext with the same Context object reads the current value from the nearest matching Provider above it.

7. Reading Updated Context Values

When the Provider receives a different value, React updates the components that read that Context. The following example uses state to change the shared student information.

App.jsx
import { useState } from 'react';
import StudentContext from './StudentContext';
import StudentDetails from './StudentDetails';

function App() {
    const [student, setStudent] = useState({
        name: 'Samadhan',
        course: 'React Development',
        city: 'Pune'
    });

    function changeStudent() {
        setStudent({
            name: 'Samiksha',
            course: 'Data Analytics',
            city: 'Mumbai'
        });
    }

    return (
        <StudentContext.Provider value={student}>
            <StudentDetails />
            <button onClick={changeStudent}>
                Change Student
            </button>
        </StudentContext.Provider>
    );
}

export default App;

Use the StudentContext.jsx and StudentDetails.jsx files from Section 4 with this App.jsx example.

Expected Output

Before clicking:

Name: Samadhan

Course: React Development

City: Pune

After clicking Change Student:

Name: Samiksha

Course: Data Analytics

City: Mumbai

8. Important Rules When Reading Context

  • Import useContext from React.
  • Pass the Context object to useContext.
  • Call useContext at the top level of a component or custom Hook.
  • Place the component under the appropriate Provider when it needs a custom value.
  • Use the same Context object for the Provider and consumer.
  • Remember that the default value is used only when there is no matching Provider above the component.
  • Keep Context values and their expected data shapes consistent.

9. Common Mistakes

Mistake 1: Accessing the Wrong Property

If the Context contains student.name, use that property instead of accessing an unrelated property name.

Mistake 2: Using the Wrong Context

The Context passed to useContext must be the same Context object that the intended Provider uses.

Mistake 3: Expecting a Provider Value Outside Its Tree

Components outside a Provider's descendant tree do not receive that Provider's value.

Mistake 4: Assuming Context Automatically Stores State

Context distributes a value. If the value must change, manage it with React state or another suitable state management approach and provide the updated value.

10. Practice Tasks

Complete these tasks to practise reading Context.

  1. Create a ThemeContext with the default value 'light' and read it using useContext.
  2. Create a LanguageContext with the default value 'English'.
  3. Create a StudentContext containing a name, course, and city.
  4. Display the student name and course in two separate components.
  5. Wrap both components in the same Provider and display the shared values.
  6. Use useState to update the Context value when a button is clicked.
  7. Explain the difference between a Context default value and a Provider value.

Summary

React components can read shared information with the useContext Hook. The value comes from the closest matching Provider above the component, or from the Context default when no matching Provider exists. Multiple components can read the same Context independently, and components that consume Context receive updates when the provided value changes. Context helps reduce prop drilling and makes shared data easier to access across a component tree.