Skip to content
HooksChapter 18 / 37Intermediate4 min read

useContext

React useContext hook se prop drilling ke baghair data share karna seekhein, React 19 ke Context as provider syntax ke saath, Roman Urdu mein.

On this page

1. useContext kya hai?

Normally data props ke zariye parent se child tak jata hai. Lekin agar ek value (jaise theme ya logged-in user) bohot saare components ko chahiye, to har level par props pass karna thaka dene wala kaam hai.

Context ek "broadcast" jaisa hai. Upar ek jagah value rakh do, aur neeche jis component ko chahiye woh useContext se utha le.

2. Kyun zaroori hai?

Is problem ko prop drilling kehte hain: beech wale components woh props receive aur forward karte hain jo unhein khud chahiye hi nahi.

Prop drillingContext ke saath
App se Layout ko theme propApp ek dafa Context mein theme rakhta hai
Layout se Sidebar ko theme propSidebar ko kuch pass nahi karna
Sidebar se Button ko theme propButton khud useContext se theme parhta hai
Naya level add karo to har jagah changeBeech ke components untouched rehte hain

3. Kahan use hota hai?

Use caseContext mein kya hota hai
Theme"light" ya "dark" mode
AuthCurrent user aur logout function
LanguageSelected locale, jaise "ur" ya "en"
Design systemForm ya table ka shared size aur variant

Context un values ke liye best hai jo kam change hoti hain aur bohot jagah chahiye. Har cheez ko Context mein daalna zaroori nahi.

4. Kaise kaam karta hai?

Teen steps hain: Context banao, value provide karo, aur neeche read karo.

Context ka flow
createContext se ThemeContext banao
Upar ThemeContext value ke saath wrap karo
Neeche kisi bhi component mein useContext(ThemeContext)
React sab se qareebi provider ki value deta hai

Jab provider ki value change hoti hai, to woh tamam components re-render hote hain jo us Context ko parh rahe hain. Isi liye value ko soch samajh kar design karein.

5. Code example

Theme Context

tsx
import { createContext, useContext, useState } from "react";

type Theme = "light" | "dark";

const ThemeContext = createContext<Theme>("light");

export default function App() {
  const [theme, setTheme] = useState<Theme>("light");

  return (
    <ThemeContext value={theme}>
      <button onClick={() => setTheme(theme === "light" ? "dark" : "light")}>
        Theme badlo
      </button>
      <Toolbar />
    </ThemeContext>
  );
}

function Toolbar() {
  return <SaveButton />;
}

function SaveButton() {
  const theme = useContext(ThemeContext);
  return <button className={`btn-${theme}`}>Save</button>;
}

Code kaise kaam kar raha hai?

LineMatlab
createContext<Theme>("light")Context banaya, "light" default value hai
<ThemeContext value={theme}>Neeche ke tamam components ko theme mil gaya
ToolbarBeech wala component, isne koi prop pass nahi kiya
useContext(ThemeContext)SaveButton ne seedha theme parh li

Custom hook ke saath clean pattern

tsx
const AuthContext = createContext<{ user: string | null } | null>(null);

export function useAuth() {
  const ctx = useContext(AuthContext);
  if (!ctx) {
    throw new Error("useAuth ko AuthProvider ke andar use karein");
  }
  return ctx;
}

Yeh pattern galat jagah use hone par foran error deta hai, aur baqi code mein sirf useAuth() likhna parta hai.

6. Common mistakes

Galati: Provider ke bahar useContext call karna

Why? Provider na mile to React default value deta hai, aur bug chupa rehta hai.

Behtar: Component ko provider ke andar rakhein, ya custom hook mein null check karke error throw karein.

Galati: Har render par naya object value mein dena

Why? Naya object har dafa different hota hai, is liye tamam consumers bina wajah re-render hote hain.

Behtar: Value ko useMemo mein rakhein, ya React Compiler use karein jo yeh khud handle karta hai.

Galati: Har state ko Context mein daal dena

Why? Tez change hone wali state se poora tree re-render hota hai aur app slow lagti hai.

Behtar: Local state local rakhein. Context sirf shared, kam change hone wali values ke liye rakhein.

Interview tip

Agar poocha jaye "Context Redux ki jagah le sakta hai?", to jawab: Context sirf data pass karne ka tareeqa hai, state management nahi. Bari, complex state ke liye useReducer + Context ya Zustand jaisi library behtar hai.

7. Pehle aur baad mein kya seekhein?

Final quick revision

PointDetail
Kya haiTree mein upar wali value neeche directly parhna
ProblemProp drilling
Banane ka tareeqacreateContext(defaultValue)
Provide karna (React 19)<MyContext value={...}>
ParhnauseContext(MyContext) ya use(MyContext)
Re-renderValue change hone par tamam consumers