Skip to content
HooksChapter 21 / 37Intermediate4 min read

Custom Hooks

React custom hooks se repeat hone wali stateful logic ko reusable functions mein nikalna seekhein, naming rules aur real examples ke saath, Roman Urdu mein.

On this page

1. Custom Hooks kya hain?

Jab do ya zyada components mein same logic ho, jaise online status check karna ya window size track karna, to woh logic ek custom hook mein nikal di jati hai.

Har component jo hook call karta hai, usay apni alag state milti hai. Hook logic share karta hai, state nahi.

2. Kyun zaroori hain?

Bina custom hookCustom hook ke saath
Har component mein same useState aur useEffectLogic ek hook mein, sab jagah ek line
Bug fix teen jagah karna parta haiFix ek jagah, sab components theek
Component lamba aur confusingComponent sirf UI par focus
Logic test karna mushkilHook ko alag se test kar sakte hain

3. Kahan use hota hai?

Hook ka naamKaam
useOnlineStatusUser online hai ya offline
useLocalStorageState ko localStorage ke saath sync
useDebounceTyping rukne ke baad value update
useWindowSizeScreen ki width aur height
useAuthContext se current user nikalna

4. Kaise kaam karta hai?

Logic nikalne ka process
Do components mein same hooks wali logic dekho
Usay use se shuru hone wale function mein move karo
Zaroori values return karo
Components mein hook call karo
Har call ko apni alag state milti hai

Custom hook par bhi Rules of Hooks lagte hain: top level par call karein, loop ya condition ke andar nahi.

5. Code example

useOnlineStatus

tsx
import { useEffect, useState } from "react";

export function useOnlineStatus() {
  const [isOnline, setIsOnline] = useState(true);

  useEffect(() => {
    const update = () => setIsOnline(navigator.onLine);
    update();
    window.addEventListener("online", update);
    window.addEventListener("offline", update);
    return () => {
      window.removeEventListener("online", update);
      window.removeEventListener("offline", update);
    };
  }, []);

  return isOnline;
}

Do components mein use

tsx
import { useOnlineStatus } from "./use-online-status";

export function StatusBar() {
  const isOnline = useOnlineStatus();
  return <p>{isOnline ? "Online" : "Offline"}</p>;
}

export function SaveButton() {
  const isOnline = useOnlineStatus();
  return <button disabled={!isOnline}>Save</button>;
}

useDebounce

tsx
import { useEffect, useState } from "react";

export function useDebounce<T>(value: T, delay = 400) {
  const [debounced, setDebounced] = useState(value);

  useEffect(() => {
    const id = setTimeout(() => setDebounced(value), delay);
    return () => clearTimeout(id);
  }, [value, delay]);

  return debounced;
}

Code kaise kaam kar raha hai?

CheezMatlab
useOnlineStatususe prefix se React aur linter ko pata chalta hai yeh hook hai
useEffect cleanupComponent hatne par listeners remove
return isOnlineHook sirf zaroori value deta hai
Do componentsDono ki apni alag isOnline state
useDebounce<T>Generic hook, kisi bhi type ki value ke saath

6. Common mistakes

Galati: Function ka naam use se shuru na karna

Why? Linter Rules of Hooks check nahi kar pata aur andar hooks use karna galat lagta hai.

Behtar: Hooks call karne wale har function ka naam useSomething rakhein.

Galati: Har function ko hook bana dena

Why? Agar andar koi hook use nahi ho raha to yeh sirf normal helper function hai.

Behtar: Bina hooks wali logic ke liye simple function likhein, jaise formatPrice.

Galati: Samajhna ke custom hook state share karta hai

Why? Har call ki apni state hoti hai, ek component ka change dusre ko nahi dikhta.

Behtar: State share karni ho to lifting state up ya Context use karein.

Interview tip

Achha custom hook ek kaam karta hai aur naam se pata chal jata hai ke kya karta hai. useMount jaise generic lifecycle hooks se bachein, kyunke yeh React ke model ke khilaf hain.

7. Pehle aur baad mein kya seekhein?

Final quick revision

PointDetail
Kya haiuse se shuru hone wala function jo hooks use kare
Kya share hota haiLogic, state nahi
RulesRules of Hooks yahan bhi lagu
ReturnJo bhi zaroori ho: value, array ya object
FaidaReusable, testable, saaf components