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

Lifting State Up

Learn how to share state between React components by moving state to their nearest common parent, passing data through props, and updating shared state using callback functions.

1. What Is Lifting State Up?

Lifting State Up is a React technique in which state is moved from child components to their nearest common parent component. The parent manages the shared state and passes the required data to child components using props.

For example, at CIIT Training Institute, SAMADHAN and SAMIKSHA may need to display the same selected course. Instead of maintaining separate course states, their common parent can manage one shared state.

Simple Definition: Lifting State Up means moving shared state to the nearest common parent so multiple child components can use the same data.
const [course, setCourse] = useState("React");

// Pass the same state value to both children
<Samadhan course={course} />
<Samiksha course={course} />

Both components receive the same course value from their parent.

2. Why Do We Need Lifting State Up?

Sometimes multiple components need the same information. If each component maintains its own independent state, updating one component will not automatically update the other.

Without Lifting State Up
SAMADHAN course = "Java"
↕
SAMIKSHA course = "React"

Each component maintains separate state, so the values can become different.

With Lifting State Up
Parent Component course = "Java"
↓ Props
SAMADHAN course = "Java"
and
SAMIKSHA course = "Java"

Both children receive the same state value from the parent.

Remember: When components need to stay synchronized, keep the shared state in their nearest common parent.

3. Example Without Lifting State Up

In this example, SAMADHAN and SAMIKSHA maintain separate course states. Changing SAMADHAN's course does not change SAMIKSHA's course.

SeparateCourseState.jsx JSX
import { useState } from "react";

function Samadhan() {
    const [course, setCourse] = useState("React");

    return (
        <div>
            <h3>SAMADHAN</h3>
            <p>Course: {course}</p>

            <button onClick={() => setCourse("Java")}>
                Select Java
            </button>
        </div>
    );
}

function Samiksha() {
    const [course, setCourse] = useState("React");

    return (
        <div>
            <h3>SAMIKSHA</h3>
            <p>Course: {course}</p>
        </div>
    );
}

function App() {
    return (
        <div>
            <Samadhan />
            <Samiksha />
        </div>
    );
}

export default App;

Expected Browser Output

Initial Output

SAMADHAN

Course: React

SAMIKSHA

Course: React

Problem: When SAMADHAN clicks Select Java, only SAMADHAN's state changes. SAMIKSHA continues to display React because the two components have independent state.

4. Moving State to the Parent

We can solve the problem by moving the course state into the common parent component, App. The parent passes the course value to both children using props.

LiftingStateUp.jsx JSX
import { useState } from "react";

function Samadhan({ course }) {
    return (
        <div>
            <h3>SAMADHAN</h3>
            <p>Selected Course: {course}</p>
        </div>
    );
}

function Samiksha({ course }) {
    return (
        <div>
            <h3>SAMIKSHA</h3>
            <p>Selected Course: {course}</p>
        </div>
    );
}

function App() {
    const [course, setCourse] = useState("React");

    return (
        <div>
            <h2>CIIT Training Institute</h2>

            <Samadhan course={course} />
            <Samiksha course={course} />
        </div>
    );
}

export default App;

How Does It Work?

  1. The parent component creates state using useState().
  2. The parent stores the selected course in course.
  3. The parent passes the value to SAMADHAN through props.
  4. The parent also passes the same value to SAMIKSHA.
  5. Both children display the same selected course.
Expected Browser Output
CIIT Training Institute

SAMADHAN

Selected Course: React

SAMIKSHA

Selected Course: React

Important: This example shares the same state value between components. To change that value from a child, pass a callback function from the parent, as shown in the next example.

5. Updating Shared State Using Callback Functions

A child component cannot directly access a parent's local state. The parent can pass a callback function to the child. When the child calls that function, the parent updates its own state.

Data Flow:

Parent passes state and functions down to children through props. A child calls a callback to request a state update in the parent.

SharedCourseState.jsx JSX
import { useState } from "react";

function Samadhan({ course }) {
    return (
        <div>
            <h3>SAMADHAN</h3>
            <p>Selected Course: {course}</p>
        </div>
    );
}

function Samiksha({ onChangeCourse }) {
    return (
        <div>
            <h3>SAMIKSHA</h3>

            <button onClick={() => onChangeCourse("React")}>
                Select React
            </button>

            <button onClick={() => onChangeCourse("Java")}>
                Select Java
            </button>

            <button onClick={() => onChangeCourse("Testing")}>
                Select Testing
            </button>
        </div>
    );
}

function App() {
    const [course, setCourse] = useState("React");

    return (
        <div>
            <h2>CIIT Course Selection</h2>

            <Samadhan course={course} />

            <Samiksha
                onChangeCourse={setCourse}
            />
        </div>
    );
}

export default App;

Expected Browser Behavior

Example Interaction

Initially: SAMADHAN sees React.

Click Select Java: The parent state changes to Java.

After update: SAMADHAN sees Java.

SAMIKSHA's buttons remain available because the parent owns the selected course.

Important: The child calls onChangeCourse("Java"). The parent provided setCourse as that callback, so the parent's state is updated.

6. Lifting State Up with Multiple Components

Shared state can be used by more than two components. In this example, the parent manages the course, a selector changes it, and two display components show the updated value.

MultipleSharedComponents.jsx JSX
import { useState } from "react";

function CourseDisplay({ course }) {
    return (
        <div>
            <h3>SAMADHAN</h3>
            <p>Selected Course: {course}</p>
        </div>
    );
}

function CourseSelector({ onSelect }) {
    return (
        <div>
            <button onClick={() => onSelect("React")}>
                React
            </button>

            <button onClick={() => onSelect("Software Testing")}>
                Software Testing
            </button>

            <button onClick={() => onSelect("Data Analytics")}>
                Data Analytics
            </button>
        </div>
    );
}

function CourseSummary({ course }) {
    return (
        <div>
            <h3>SAMIKSHA</h3>
            <p>Course Summary: {course}</p>
        </div>
    );
}

function App() {
    const [course, setCourse] = useState("React");

    return (
        <div>
            <h2>CIIT Training Institute</h2>

            <CourseDisplay course={course} />

            <CourseSelector onSelect={setCourse} />

            <CourseSummary course={course} />
        </div>
    );
}

export default App;

Component Relationship

App (Parent) useState("React")
↓ Props
CourseDisplay Displays course
CourseSelector Calls onSelect()
CourseSummary Displays course
↑ CourseSelector requests an update through a callback
Result: Selecting Software Testing updates the parent state. Both SAMADHAN's course display and SAMIKSHA's course summary receive the new value through props.

7. Understanding State, Props, and Callbacks

Concept Purpose Example
State Stores the shared value in the parent. useState("React")
Props Passes data from parent to child. course={course}
Callback Lets a child request an update. onSelect={setCourse}
State Setter Requests an update to parent state. setCourse("Java")
Remember: Props are read-only. A child should not directly modify a prop. Instead, the child can call a callback supplied by the parent.

8. Common Mistakes

Maintaining Separate State for Shared Data

If two components must display the same selected course, separate state variables can become inconsistent.

Solution: Keep the shared state in their nearest common parent.

Forgetting to Pass Props

Avoid:

<Samadhan />

Use:

<Samadhan course={course} />

Calling a Callback During Rendering

Avoid:

<button onClick={onSelect("Java")}>
    Select Java
</button>

Use:

<button onClick={() => onSelect("Java")}>
    Select Java
</button>

The second example passes a function that executes when the button is clicked.

Trying to Change Parent State Directly

A child should not try to access the parent's local state directly. The parent should provide a callback function to request changes.

9. Practice Questions

  1. What is Lifting State Up in React?
  2. Why do we move shared state to the nearest common parent?
  3. What problem can occur when two components maintain separate state?
  4. How does a parent pass state to child components?
  5. What is the role of a callback function?
  6. Create two components that display the same selected course.
  7. Update the selected course from a child using a callback.
  8. Explain the difference between state and props.
  9. Create a CIIT course selector with React, Software Testing, and Data Analytics.

Summary

Lifting State Up means moving shared state to the nearest common parent component. The parent manages the state using useState() and passes the required values to children through props. When a child needs to update shared state, it calls a callback function provided by the parent. React then updates the relevant components with the new value. This approach helps multiple components stay synchronized and makes state management easier to understand.