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 createContext

Learn how to create a Context object in React and prepare shared data for use across multiple components.

1. What Is createContext?

createContext is a React function used to create a Context object. This object allows components to access a shared value through a matching Context Provider.

For example, if several components need the same student name, theme, or language setting, you can create a Context for that information instead of passing it through every intermediate component.

Syntax: Call createContext(defaultValue) outside your component definitions, usually near the top of a separate Context file.

2. Basic Syntax

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

const StudentContext = createContext(null);

export default StudentContext;

Explanation

  • createContext is imported from React.
  • StudentContext is the Context object created by React.
  • null is the default value in this example.
  • The Context object can be exported and imported into other files.
The default value is used when a component reads this Context without a matching Provider above it. It is not automatically updated by changing a Provider value.

3. Creating a Student Context

Let's create a Context that will eventually share student information with components in a training institute portal.

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

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

export default StudentContext;

Understanding the Default Value

The default value is an object containing a student name and course. A component reading this Context without a matching Provider can receive this default object.

Default Student: Guest Student

Default Course: Not Selected

In a later lesson, we will provide actual values through a Context Provider.

4. Understanding the Context Flow

Import createContext
↓
Create StudentContext
↓
Export Context
↓
Provide a Value
↓
Read Value in Components

Creating a Context only establishes the Context object. Components need a matching Provider to receive a supplied value. We will learn about Providers in the upcoming lesson.

5. Using a Default Context Value

The following example shows how a component can read the default value of a Context using useContext.

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

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

export default StudentContext;
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 output uses the default value because the example does not place Student inside a matching Provider. The next lessons explain how a Provider supplies custom values.

6. Important Points to Remember

  • Import createContext from React.
  • Use createContext(defaultValue) to create a Context object.
  • The default value can be a string, number, object, or another suitable value.
  • Export the Context object when other files need to use it.
  • A matching Provider supplies the value to descendant components.
  • useContext reads the nearest matching Provider value or the default value.

7. Practice Tasks

Complete these tasks to practise creating Context objects.

  1. Create a Context named ThemeContext with the default value 'light'.
  2. Create a Context named LanguageContext with the default value 'English'.
  3. Create a StudentContext with a default object containing a name and course.
  4. Export a Context from one file and import it into another component.
  5. Explain the difference between a Context default value and a value supplied by a Provider.

Summary

The createContext function creates a Context object that can be used to share values between React components. Its optional default value is used when no matching Provider exists above the component reading the Context. After creating Context, export it for use in other files. In the next lesson, we will learn how useContext reads Context values inside components.