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

Understanding useContext

Learn how to read shared Context values inside React functional components using the useContext Hook.

1. What Is useContext?

useContext is a React Hook that allows a functional component to read the current value of a Context. It helps components access shared information without receiving that information through props at every level.

For example, a student dashboard, profile page, and course page may all need information about the current student. Context can make that information available to these components.

Remember: createContext creates the Context object, while useContext reads its value inside a component.

2. Syntax of useContext

Syntax.jsx
import { useContext } from 'react';

const value = useContext(MyContext);

Explanation

  • Import useContext from React.
  • Pass the Context object to useContext.
  • React returns the value from the closest matching Provider above the component.
  • If no matching Provider exists, React returns the Context's default value.

3. Create a Context

First, create a Context that stores a student's name and course. We will use this Context in our component.

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

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

export default StudentContext;

The default object contains the values that will be returned when a component reads this Context without a matching Provider above it.

4. Read Context Using useContext

Now import the Context into a functional component and read its value using useContext.

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

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

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

export default Student;

Expected Output

Student Details

Name: Guest Student

Course: Not Selected

This example uses the default Context value because no matching Provider has been added yet. The next lesson explains how a Provider supplies custom values.

5. Read a Custom Value from a Provider

A Provider can supply a custom value to components inside its tree. Here is a complete example using the traditional Context.Provider syntax, which is supported by React versions including React 18.

App.jsx
import StudentContext from './StudentContext';
import Student from './Student';

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

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

export default App;

Keep the previous StudentContext.jsx and Student.jsx files. Use this App.jsx as the parent component for the example.

Expected Output

Student Details

Name: Samadhan

Course: React Development

The Student component reads the supplied object through useContext. It does not need to receive the student object as a prop.

6. How useContext Works

Create Context
↓
Provider Supplies Value
↓
Child Calls useContext
↓
Component Uses Value

When the Provider's value changes, React updates components that read that Context. The component receives the value from the closest matching Provider above it.

7. Common Mistakes

Mistake 1: Forgetting the Import

Import useContext from React before using it.

Mistake 2: Passing the Wrong Argument

Pass the Context object, such as StudentContext, rather than a string containing its name.

Mistake 3: Missing a Provider

Without a matching Provider, a component receives the Context default value. If that default is null, accessing properties on it can cause an error.

Mistake 4: Calling useContext Conditionally

Call useContext at the top level of a React function component or custom Hook, not inside an if statement or loop.

8. Practice Tasks

Complete these tasks to strengthen your understanding.

  1. Create a ThemeContext with the default value 'light'.
  2. Use useContext to read the theme inside a component.
  3. Create a LanguageContext with the default value 'English'.
  4. Provide a custom student object using StudentContext.Provider.
  5. Explain the difference between createContext and useContext.

Summary

The useContext Hook reads the current value of a React Context inside a functional component. The value comes from the closest matching Provider above the component, or from the Context default value when no matching Provider exists. Combined with createContext and a Provider, useContext helps share data without passing props through every intermediate component.