📚
State Management

State Management Libraries

Redux, Zustand, Overview
💡 External, state, LIBRARIES, EK, "CITY-WIDE, POWER, GRID" jaisi, HAIN — Context API, EK, "SINGLE, BUILDING, KA, GENERATOR" hai (KAAM, KARTA, hai, CHHOTE, SCALE, PAR), LEKIN, BADE, COMPLEX, "CITY" (LARGE, APPLICATION) KO, EK, ZYAADA, ROBUST, aur, EFFICIENT, SYSTEM, (Redux/Zustand) CHAHIYE.

Redux, EK, PREDICTABLE, STATE, CONTAINER, hai — SINGLE, STORE, PURE, REDUCER, FUNCTIONS, aur, ACTIONS, KE, THROUGH, STATE, UPDATE, HOTI, hai. Redux DevTools SE, TIME-TRAVEL, DEBUGGING, POSSIBLE, hai — BADI, TEAMS, aur, COMPLEX, APPS, MEIN, POPULAR.

Zustand, EK, MODERN, LIGHTWEIGHT, ALTERNATIVE, hai — MINIMAL, BOILERPLATE, (NO, PROVIDERS, NO, ACTIONS, KI, ZAROORAT) KE, SAATH, GLOBAL, STATE, MANAGE, KARNE, DETA, hai. React Query/TanStack Query, SERVER, STATE, (API, DATA, CACHING) KE, LIYE, SPECIALIZED, hai — CLIENT, STATE, (Redux/Zustand) SE, ALAG, CONCERN.

// Zustand — MINIMAL boilerplate:
import { create } from "zustand";

const useCartStore = create((set) => ({
  items: [],
  addItem: (item) => set((state) => ({ items: [...state.items, item] })),
}));

// Kisi bhi component mein, DIRECTLY:
function CartBadge() {
  const items = useCartStore((state) => state.items);
  return <span>{items.length} items</span>;
}
📚
External, state, LIBRARIES, EK, "CITY-WIDE, POWER, GRID" jaisi, HAIN — Context API, EK, "SINGLE, BUILDING, KA, GENERATOR" hai (KAAM, KARTA, hai, CHHOTE, SCALE, PAR), LEKIN, BADE, COMPLEX, "CITY" (LARGE, APPLICATION) KO, EK, ZYAADA, ROBUST, aur, EFFICIENT, SYSTEM, (Redux/Zustand) CHAHIYE.
1 / 2
⚡ झट से Recap
  • Redux = predictable, PREDICTABLE, single-store, LARGE apps/teams ke, liye
  • Zustand = MODERN, lightweight, MINIMAL boilerplate
  • Server state (React Query) vs client state (Redux/Zustand) — ALAG, CONCERNS
इस page में (2 subtopics)

Redux Toolkit (RTK), REDUX, KA, "OFFICIAL, RECOMMENDED" WAY, hai, LIKHNE, KA — createSlice(), BOILERPLATE, (ACTION, TYPES, ACTION, CREATORS) DRASTICALLY, KAM, KAR, DETA, hai, aur, "MUTATING" SYNTAX, (Immer, KE, THROUGH) ALLOW, KARTA, hai, BINA, ACTUALLY, STATE, MUTATE, KIYE.

const counterSlice = createSlice({
  name: "counter",
  initialState: { value: 0 },
  reducers: {
    increment: (state) => { state.value += 1; }   // "mutating" syntax, Immer, se, safe
  }
});

Chhoti, apps = useState/useReducer, kaafi, hain. Medium, apps = Context, +, useReducer. Large, apps, WITH, complex, client, state = Redux Toolkit, ya, Zustand. Server, data, (API, responses) = React Query/TanStack Query, HAMESHA, RECOMMENDED.

  • Chhoti apps = useState/useReducer
  • Medium apps = Context + useReducer
  • Large apps = Redux Toolkit/Zustand + React Query (server state)