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

State Management Introduction

1. What Is State Management?

State management is the process of storing, updating and managing data that changes over time in a React application. State determines what information is displayed on the screen and how the user interface responds to user interactions.

For example, a counter value, logged-in user information, shopping cart items and selected theme can all be managed through state.

Remember: When React state changes, React can re-render the component to display the updated information.

2. Why Is State Management Important?

  • Stores changing application data.
  • Updates the user interface when data changes.
  • Maintains user selections and input values.
  • Helps share information between components.
  • Makes complex applications easier to organize.
  • Supports predictable application behavior.

3. Understanding State with an Example

Consider a counter application. The counter value changes when the user clicks the Increment or Decrement button. React state stores the current value and updates the displayed number.

Counter.jsx
import { useState } from "react";

function Counter() {
    const [count, setCount] = useState(0);

    return (
        <div>
            <h2>Counter: {count}</h2>

            <button onClick={() => setCount(count + 1)}>
                Increment
            </button>

            <button onClick={() => setCount(count - 1)}>
                Decrement
            </button>
        </div>
    );
}

export default Counter;
Explanation: useState(0) initializes the counter to zero. The setCount() function updates the state, and React renders the updated value.

4. Types of State in React

State Type Description Example
Local State Used within a particular component. Counter value
Shared State State shared between related components. Selected product
Global State Application-wide state accessible to multiple components. Logged-in user
Server State Data fetched and managed from a server. Product list from an API
URL State State represented in the URL. Search query or page number
Note: These are useful categories for understanding application state. They do not all require the same state management tool.

5. Local State vs Global State

Local State

  • Belongs to a component.
  • Useful for component-specific behavior.
  • Commonly managed with useState.

Example: Show or hide a password field.

Global State

  • Can be accessed by multiple components.
  • Useful for shared application information.
  • Can use Context or Redux Toolkit.

Example: Shopping cart shared across pages.

6. State Management Flow

Initial State
↓
User Interaction
↓
State Update
↓
React Re-renders the UI
↓
Updated Information Displayed

7. Managing State with useState

The useState Hook is a built-in React Hook used to add state to function components.

useState Syntax
const [state, setState] = useState(initialValue);
  • state: The current state value.
  • setState: The function used to update the state.
  • initialValue: The initial state value.
Example: Managing a Name
import { useState } from "react";

function Student() {
    const [name, setName] = useState("Samadhan");

    return (
        <div>
            <h2>Student Name: {name}</h2>

            <button onClick={() => setName("Samiksha")}>
                Change Name
            </button>
        </div>
    );
}

export default Student;

8. When Should You Use Global State?

Not every value needs to be stored globally. Start with local state and move to shared or global state when multiple components need access to the same information.

  • Authentication information used throughout the application.
  • Shopping cart data shared across pages.
  • Application-wide theme preferences.
  • Shared filters or selections used by multiple components.
Best practice: Avoid making all state global. Keep component-specific state local whenever possible.

9. State Management Tools

Tool Purpose
useState Manages local component state.
useReducer Manages state through a reducer function.
Context API Shares values across components without passing props through every intermediate level.
Redux Toolkit Provides structured management for shared application state.
Server-state libraries Help manage fetching, caching and synchronizing server data.

10. Practice Tasks

  1. Create a counter component using useState.
  2. Add Increment and Decrement buttons.
  3. Create a component that displays a student's name.
  4. Add a button to change the student's name.
  5. Create a show/hide feature for a paragraph.
  6. Identify which values in a shopping application should be local or global state.
  7. Explain when Context API or Redux Toolkit may be useful.

Summary

State management is an important part of React development because it controls how changing data is stored and displayed. Local state is suitable for component-specific information, while shared and global state help multiple components access common data. React provides Hooks such as useState and useReducer, along with Context, to support different state management requirements.