TechIT TECH IN HINDI
Banti Kevat — Founder TechIT
Founder & Author

Banti Kevat

M.Tech AI Student · Full-Stack Developer · Ujjain, MP 🇮🇳

React, MERN aur AI ko simple Hindi mein sikhaata hoon. Real projects banaye hain — Seva Setu (hyperlocal services) aur UjjainTemple (devotional web). Seekha hua sach mein kaam aata hai. 🚀

0+ Articles
0+ Categories
0% Free
हिंदीEasy Language
✅ Free Forever 🇮🇳 Tech in Hindi 👨‍💻 Real Developer 🚫 No Spam 🎓 M.Tech AI
React useMemo Hook Kya Hai aur Kaise Use Karein (in Hindi)
Coding info

React useMemo Hook Kya Hai aur Kaise Use Karein (in Hindi)

TechIT — Banti Kevat जुलाई 11, 2026 5 min read
Listen to this article (Hindi/English)
React useMemo Hook Kya Hai aur Kaise Use Karein (in Hindi)

✨ Quick Answer: React useMemo hook kya hai aur kaise use karein? Hindi mein seekhein kaise useMemo hook se React app ki performance ko optimize aur fast banayein.

React useMemo Hook क्या है और इसे कैसे use करें? इस detailed guide में heavy calculations को memoize करके app performance बढ़ाना सीखें बिल्कुल आसान भाषा में।

मेरे प्यारे डेवलपर दोस्तों, क्या आपके साथ कभी ऐसा हुआ है कि आपने एक शानदार ReactJS application बनाई, लेकिन जैसे ही user ने किसी search box में टाइप किया या किसी filter पर click किया, आपकी पूरी app lag करने लगी? आप सोचने लगते हैं कि आखिर कोड में ऐसा क्या गलत हो गया! एक सीनियर डेवलपर होने के नाते, मैंने अपने करियर में ऐसे performance bottlenecks हजारों बार देखे हैं। अक्सर इसका सबसे बड़ा कारण होता है—unnecessary re-renders के दौरान heavy calculations का बार-बार चलना।

इसी समस्या को चुटकियों में हल करने के लिए React हमारे लिए लेकर आया है React useMemo Hook। चलिए, आज हम और आप साथ मिलकर बैठते हैं और इस hook की पूरी कुंडली खोलते हैं, ताकि आज के बाद आपको performance optimization में कभी कोई परेशानी न आए।

⚡ Quick Answer: React useMemo Hook एक built-in tool है जो किसी heavy function के calculations को cache (memoize) कर लेता है। यह तब तक उस calculated value को दोबारा compute नहीं करता जब तक उसकी dependency change न हो। इससे unnecessary operations रुकते हैं और application super-fast हो जाती है।
---

React useMemo Hook क्या है और यह काम कैसे करता है?

सरल शब्दों में कहें तो useMemo का काम है "याद रखना" (Memoization)। Memoization computer science का एक core concept है, जिसमें हम किसी expensive function call के result को cache कर लेते हैं और अगली बार सेम input मिलने पर calculate करने के बजाय cached memory से direct value return कर देते हैं।

React में जब भी किसी React components का state या prop बदलता है, तो वह component पूरा का पूरा re-render होता है। अब मान लीजिए उस component के अंदर एक ऐसा function है जो 10 लाख elements के array को filter करता है। री-रेंडर होने पर वह heavy operation हर बार दोबारा चलेगा, भले ही उस filter का state से कोई लेना-देना न हो! यहीं पर React useMemo Hook एंट्री मारता है और कहता है, "रुको भाई! मैं इस calculation को अपने पास संभालकर रखता हूँ। जब तक dependency नहीं बदलेगी, तब तक मैं पुराना cached result ही वापस दूँगा।"

🏗️ useMemo Performance Flow Diagram
State Triggered Re-render
Has Dependency Changed?
YES: Recalculate & Cache
/
NO: Return Cached Value
Diagram: React render cycle optimization using useMemo cache checking process
---

हमें React useMemo Hook का उपयोग कब करना चाहिए?

दोस्त, यह बहुत ही महत्वपूर्ण सवाल है। बहुत से नए डेवलपर्स हर जगह `useMemo` ठूंस देते हैं, जिससे application fast होने के बजाय memory consumption के कारण slow हो जाती है। इसे केवल इन स्थितियों में use करें:

  • Expensive Calculations: जब आपके पास कोई ऐसा computational function हो जो CPU का काफी समय लेता है (जैसे heavy loops, sorting, mathematical computations, deep tree parsing, structure modifications)।
  • Skipping Re-renders of Child Components: जब आप किसी object या array को prop के रूप में child component में भेज रहे हों। चूंकि JavaScript में `{}` और `[]` हर render पर नया reference बनाते हैं, child components बिना बात के re-render होते हैं।
  • Large Data Filtering: जब API से मिले बहुत बड़े dataset को dynamic input के आधार पर filter या format करना हो।
---

React useMemo Hook का Syntax और Implementation कैसे करें?

चलिए, सबसे पहले इसका बुनियादी syntax समझते हैं। यह दिखने में काफी हद तक `useEffect` जैसा ही है:

const memoizedValue = useMemo(() => {
  // Expensive calculation function यहाँ लिखें
  return computeExpensiveValue(a, b);
}, [a, b]); // Dependency array

यहाँ मुख्य रूप से दो चीजें हैं:

  1. Create Function: पहला argument एक anonymous function होता है जो optimized computation करता है और value return करता है।
  2. Dependency Array: दूसरा argument वह variables की list होती है, जिनपर आपका calculation depend करता है। जब इनमें से कोई भी variable बदलेगा, तभी function दोबारा execute होगा।
---

बिना useMemo के Performance Issue (The Problem Code)

बात को और गहराई से समझने के लिए, चलिए एक production-ready component लिखते हैं। यहाँ हमारे पास एक calculation है जो बहुत heavy है (10,000,0000 loops!) और एक simple counter है। आप खुद महसूस करेंगे कि counter button click करने पर भी app lag होगी क्योंकि heavy function हर render पर run हो रहा है।

import React, { useState } from 'react';

// यह एक heavy function है जो CPU-intensive task simulate करता है
const expensiveCalculation = (num) => {
  console.log("Expensive computation running...");
  let count = 0;
  for (let i = 0; i < 1000000000; i++) {
    count += 1;
  }
  return num + count;
};

const UnoptimizedComponent = () => {
  const [count, setCount] = useState(0);
  const [number, setNumber] = useState(5);

  // यह function हर render पर जबरदस्ती दोबारा चलेगा!
  const calculatedResult = expensiveCalculation(number);

  return (
    <div style={{ padding: '20px', maxWidth: '600px', margin: 'auto' }}>
      <h2>Unoptimized Component (Problem Demo)</h2>
      <div>
        <h3>Number-based Calculation: {calculatedResult}</h3>
        <button 
          onClick={() => setNumber(prev => prev + 1)}
          style={{ padding: '8px 16px', background: '#0284c7', color: '#fff', border: 'none', cursor: 'pointer', marginRight: '10px' }}
        >
          Increment Number
        </button>
      </div>

      <div style={{ marginTop: '20px' }}>
        <h3>Counter (Unrelated State): {count}</h3>
        <button 
          onClick={() => setCount(prev => prev + 1)}
          style={{ padding: '8px 16px', background: '#ec4899', color: '#fff', border: 'none', cursor: 'pointer' }}
        >
          Increment Counter
        </button>
        <p>Notice: जब आप Counter button दबाते हैं, तो भी screen lag होती है क्योंकि heavy calculations फिर से चलती हैं!</p>
      </div>
    </div>
  );
};

export default UnoptimizedComponent;
---

useMemo के साथ Performance Issue Solve करना (The Optimized Code)

अब, चलिए अपने "Unoptimized" कोड को React useMemo Hook के साथ optimize करते हैं ताकि फालतू का computation बंद हो सके। नीचे दिए गए code को ध्यान से देखिए:

import React, { useState, useMemo } from 'react';

const expensiveCalculation = (num) => {
  console.log("Optimized computation running only when number changes...");
  let count = 0;
  for (let i = 0; i < 1000000000; i++) {
    count += 1;
  }
  return num + count;
};

const OptimizedComponent = () => {
  const [count, setCount] = useState(0);
  const [number, setNumber] = useState(5);

  // useMemo का जादू! 
  // अब calculatedResult केवल तभी recalculate होगा जब 'number' state बदलेगी।
  // Counter state change होने पर cached result return होगा!
  const calculatedResult = useMemo(() => {
    return expensiveCalculation(number);
  }, [number]); // Dependency is 'number'

  return (
    <div style={{ padding: '20px', maxWidth: '600px', margin: 'auto', border: '1px solid #e2e8f0', borderRadius: '10px' }}>
      <h2 style={{ color: '#0f172a' }}>Optimized Component with useMemo</h2>
      
      <div style={{ background: '#f8fafc', padding: '15px', borderRadius: '8px', marginBottom: '15px' }}>
        <h3>Calculation Result: <span style={{ color: '#0284c7' }}>{calculatedResult}</span></h3>
        <button 
          onClick={() => setNumber(prev => prev + 1)}
          style={{ padding: '10px 18px', background: '#0284c7', color: '#fff', border: 'none', borderRadius: '5px', cursor: 'pointer' }}
        >
          Change Number (Triggers Heavy Work)
        </button>
      </div>

      <div style={{ background: '#fcf6f9', padding: '15px', borderRadius: '8px' }}>
        <h3>Independent Counter: <span style={{ color: '#ec4899' }}>{count}</span></h3>
        <button 
          onClick={() => setCount(prev => prev + 1)}
          style={{ padding: '10px 18px', background: '#ec4899', color: '#fff', border: 'none', borderRadius: '5px', cursor: 'pointer' }}
        >
          Increment Counter (Instant Response!)
        </button>
        <p style={{ fontSize: '13px', color: '#64748b', marginTop: '10px' }}>
          अब Counter दबाने पर application बिल्कुल भी lag नहीं करेगी, क्योंकि memoized value cache से तुरंत आ रही है।
        </p>
      </div>
    </div>
  );
};

export default OptimizedComponent;
---

React useMemo vs useCallback vs useEffect में क्या अंतर है?

React hooks की दुनिया में नए डेवलपर्स अक्सर इन तीनों hooks के बीच उलझ जाते हैं। घबराइए नहीं, मैंने आपके लिए एक बेहद सरल structural comparison table तैयार की है, जो सारी दुविधा दूर कर देगी:

Hook Name मुख्य उद्देश्य (Main Purpose) क्या Return करता है? कब Run होता है?
useMemo Heavy computations के result को memoize करना। Computed Value (Array/Object/Value) Rendering phase के दौरान ही।
useCallback Pure function references को memoize करना ताकि child re-render न हो। Memoized Function Reference itself Rendering phase के दौरान ही।
useEffect Side-effects run करना (जैसे data fetching, event listeners)। Nothing (or clean-up function) Component render होने के ठीक बाद (Paint screen)।
---

Common Mistakes: useMemo use करते समय developers क्या गलतियाँ करते हैं?

सीनियर डेवलपर होने के नाते, मेरा सबसे पहला नियम है: "Premature optimization is the root of all evil" (बिना जरूरत के पहले से optimize करने की कोशिश करना हानिकारक है)। यहाँ कुछ common errors और mistakes दी गई हैं जिनसे आपको हमेशा बचना चाहिए:

1. हर छोटी calculation के लिए useMemo का इस्तेमाल करना

अगर आप `useMemo(() => a + b, [a, b])` लिख रहे हैं, तो आप React App को slow कर रहे हैं! React को hook register करने, dependencies track करने और memory block reference allocate करने के लिए internally CPU cycles खर्च करने पड़ते हैं। एक साधारण calculations को dynamic run करना, memory cache management से बहुत सस्ता पड़ता है।

2. Dependency Array में values को भूल जाना या खाली छोड़ना

अगर आपका calculation किसी variable पर depend करता है और आपने उसे dependency array में नहीं डाला, तो useMemo हमेशा stale (पुरानी/गलत) data return करेगा। यह एक बहुत ही irritating dynamic-value rendering error पैदा करता है जिसे track करना मुश्किल होता है।

3. useMemo के अंदर side-effects (जैसे API calling) चलाना

हमेशा याद रखें, `useMemo` render-phase के दौरान चलता है। आपको इसके अंदर कभी भी fetch calls, timers या DOM modifications (Side Effects) नहीं करने चाहिए। Side effects के लिए केवल और केवल useEffect का ही इस्तेमाल करें।

---

Best Practices: Use React useMemo Like a Pro Senior Developer

अगर आप सच में professional-grade, scalable React websites बनाना चाहते हैं, तो इन principles को हमेशा फॉलो करें:

  • First Measure, Then Optimize: optimization शुरू करने से पहले, React DevTools Profiler tab का use करें। Check करें कि कौन सा component सच में render lag कर रहा है।
  • Keep hooks pure: useMemo के callbacks pure functions होने चाहिए। वे component के outside variable state को modify न करें।
  • Clean Reference Checking: जब child component की list rendering optimize करनी हो, तो useMemo का use component logic levels पर prop objects की reference consistency बनाए रखने के लिए करें।
  • Refer to Official Docs: किसी भी advanced scenario के लिए React official documentation को देखें, जहाँ updates और concurrent modes के details दिए गए हैं।
---

तो दोस्तों, हमने आज विस्तार से सीखा कि React useMemo Hook क्या है, यह किस तरह memory reference और cache systems का use करके calculations को fast बनाता है, और इसे code में सही तरीके से कैसे लागू करते हैं। useMemo एक बेहद powerful tool है लेकिन इसकी ताकत का इस्तेमाल समझदारी से ही करें। आशा है, अब आपकी application पहले से काफी ज्यादा optimize और fast रिस्पॉन्स देने लगेगी! अगली बार फिर मिलेंगे, किसी नए coding problem को चुटकियों में सुलझाने के लिए। तब तक के लिए, Happy Coding!

---

Frequently Asked Questions (FAQs)

Q1: क्या useMemo का use करने से App की performance हमेशा बढ़ती है?
नहीं! हर calculation के लिए useMemo का use करने से performance घट सकती है। Dependency tracking और cache reference setup करने के लिए React को runtime memory की आवश्यकता होती है। इसे केवल heavy computations या referential equality issues के लिए ही use करें।
Q2: useMemo और useCallback में क्या basic अंतर है?
मूलभूत अंतर यह है कि useMemo एक function के return value (result) को cache करता है, जबकि useCallback स्वयं पूरे function callback reference को cache करता है ताकि child elements trigger न हों।
Q3: क्या मैं Dependency Array को खाली [ ] छोड़ सकता हूँ?
हाँ, आप dependency array को खाली [ ] छोड़ सकते हैं। ऐसा करने पर useMemo केवल initial render पर ही function को run करेगा और पूरे component lifecycle में हमेशा वही cache value return करता रहेगा।
Q4: क्या React strict mode में useMemo दो बार चलता है?
हाँ, Development mode में React Strict Mode side-effects और bugs को spot करने के लिए components और hooks (useMemo सहित) को intentionaly दो बार trigger करता है। Production build में यह behavior स्वतः बंद हो जाता है।
Q5: क्या useMemo और React.memo एक ही चीज़ हैं?
नहीं। useMemo component के अंदर की variables/calculations को cache करने के लिए React Hook है। जबकि React.memo एक higher-order component (HOC) है जो पूरे React component को render cache (memoize) करने के लिए use किया जाता है।

Kya yeh article helpful tha?

B
Banti KevatFounder

TechIT ka founder · M.Tech AI student · Ujjain, MP. React, AI aur coding ko simple Hindi mein sikhaata hoon taaki har student aage badh sake. 🇮🇳

Advertisement

Related ArticlesAapko yeh bhi pasand aayega

Loading...

टिप्पणियाँ

Hum aapke experience ko behtar banane ke liye cookies use karte hain. Site use karke aap hamari Privacy Policy se sehmat hain.