Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 Basics – React + Next.js की नींव  ·  Lesson 07

useEffect और Side Effects

Side effect क्या है?

Component का मुख्य काम JSX return करना है। इसके अलावा जो भी बाहर की दुनिया को छूता है — timer शुरू करना, localStorage पढ़ना, API call करना, document.title बदलना — वह side effect है। useEffect ऐसा code React के screen update करने के बाद चलाता है।

Rendercomponent JSX लौटाता हैScreen updatebrowser paint करता हैuseEffect चलता हैtimer, storage, fetchCleanupअगली बार / हटने से पहले
useEffect कब चलता है

useEffect syntax

JSX
'use client'
import { useEffect, useState } from 'react'

useEffect(() => {
  // 1. code that touches the outside world
  return () => {
    // 2. optional cleanup
  }
}, [/* 3. dependencies */])

Dependency array तय करता है कब चले

आप लिखते हैंEffect चलता हैकिसके लिए
useEffect(fn, [])एक बार, पहले render के बादTimer शुरू, storage से load
useEffect(fn, [q])पहले render + जब भी q बदलेText बदलने पर search
useEffect(fn)हर render के बादकम ज़रूरत — सावधानी से

Example: cleanup वाली live घड़ी

JSX – components/Clock.jsx
'use client'
import { useEffect, useState } from 'react'

export default function Clock() {
  const [time, setTime] = useState(null) // null on the server, real time in the browser

  useEffect(() => {
    setTime(new Date())
    const id = setInterval(() => setTime(new Date()), 1000) // start
    return () => clearInterval(id)                            // cleanup: stop
  }, []) // [] = run once after the first render

  return <p>⏰ {time ? time.toLocaleTimeString('en-IN') : '--:--:--'}</p>
}
⏰ 10:42:07 am

clearInterval के बिना page छोड़ने पर भी timer background में चलता रहेगा — यह memory leak है। Cleanup function इसे रोकता है।

💡 हम null से शुरू करके time effect के अंदर set करते हैं। Server और browser की घड़ी अलग होती है, और render में सीधे time दिखाने से hydration mismatch warning आती है।

Example: localStorage में note save करना

JSX – components/Notes.jsx
'use client'
import { useEffect, useState } from 'react'

export default function Notes() {
  const [text, setText] = useState('')

  // Load once from the browser's storage
  useEffect(() => {
    setText(localStorage.getItem('note') || '')
  }, [])

  // Save every time `text` changes
  useEffect(() => {
    localStorage.setItem('note', text)
  }, [text])

  return <textarea value={text} onChange={(e) => setText(e.target.value)} rows={4} />
}

localStorage सिर्फ़ browser में होता है, इसलिए इसे useEffect के अंदर ही use करें (component के top पर नहीं, क्योंकि वह server पर भी चलता है)।

Example: fetch से live search

JSX – components/LiveSearch.jsx
'use client'
import { useEffect, useState } from 'react'

export default function LiveSearch() {
  const [q, setQ] = useState('')
  const [results, setResults] = useState([])

  useEffect(() => {
    if (!q) return setResults([])
    const controller = new AbortController()
    const timer = setTimeout(async () => {          // wait 300 ms after typing stops
      const res = await fetch(`/api/courses?q=${encodeURIComponent(q)}`, { signal: controller.signal })
      const data = await res.json()
      setResults(data.courses)
    }, 300)
    return () => { clearTimeout(timer); controller.abort() } // cancel old request
  }, [q])

  return (
    <div>
      <input value={q} onChange={(e) => setQ(e.target.value)} placeholder="Search courses" />
      <ul>{results.map((c) => <li key={c.slug}>{c.title} – ₹{c.fee}</li>)}</ul>
    </div>
  )
}
Browser (LiveSearch)Next.js /api/courses"py" type → 300 ms रुकोGET /api/courses?q=py{ courses: [...] }setResults → re-render
Debounced search: typing रुकने के बाद एक request

Next.js में data के लिए server बेहतर

जो data page का हिस्सा है (course list, blog posts), उसके लिए Server Component आसान और SEO के लिए बेहतर है — HTML में data पहले से होता है और Google पढ़ लेता है:

JSX – app/courses/page.jsx
// app/courses/page.jsx — Server Component: no useEffect, no loading spinner needed
import { searchCourses } from '@/lib/courses'

export default async function CoursesPage({ searchParams }) {
  const { q = '' } = await searchParams
  const list = searchCourses(q)          // runs on the server
  return <ul>{list.map((c) => <li key={c.slug}>{c.title}</li>)}</ul>
}

useEffect + fetch (client)

  • Page पहले खाली / spinner दिखाता है
  • Browser से extra request
  • Search engine content miss कर सकते हैं
  • Live, user वाले updates के लिए अच्छा

Server Component (await)

  • HTML data के साथ आता है
  • Loading flicker नहीं
  • SEO के लिए सबसे अच्छा
  • Page content के लिए अच्छा
Fetch करने की सही जगह चुनें

आम गलतियाँ

  • बिना dependency array के effect में state set करना → infinite render loop।
  • Dependencies छूट जाना → effect पुरानी value use करता है। Effect में use होने वाले component के हर variable को डालें।
  • Effect function को ही async बनाना → allowed नहीं। अंदर async function बनाकर call करें।
  • useEffect के बाहर window या localStorage use करना → server पर "window is not defined"।

सारांश

  • useEffect render के बाद side effects चलाता है।
  • Dependency array तय करता है कब चले; काम undo करने के लिए cleanup function return करें।
  • Timers, storage, subscriptions और live search के लिए use करें।
  • Next.js में page data Server Components में fetch करें।
✅ Practice

एक component बनाइए जो state के count के हिसाब से document.title को "(3) New messages" कर दे, और component हटने पर title वापस कर दे।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Development में useEffect दो बार क्यों चलता है?
Development में React Strict Mode components को mount, unmount और फिर mount करता है ताकि missing cleanup पकड़ में आए। Production build में ऐसा नहीं होता।
क्या Next.js में useEffect से data fetch करना चाहिए?
आमतौर पर नहीं। Server Component में async/await से data fetch करें ताकि HTML में पहले से data हो। useEffect सिर्फ़ browser वाले कामों के लिए: timers, localStorage या live search।
Dependency array भूल जाऊँ तो क्या होगा?
Effect हर render के बाद चलेगा। अगर effect state भी set करता है, तो infinite loop बन सकता है।
Cleanup function किस लिए है?
Effect ने जो शुरू किया उसे undo करता है — timer clear, event listener हटाना या request cancel — effect के दोबारा चलने या component हटने से पहले।
← Next.js Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicLists, Keys और Conditional Rendering आपकी प्रगति: 7 / 37 (19%) · Lesson 8 / 37 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें