MyInternships.in

React Hooks

React useEffect Hook

useEffect runs code after React has rendered and committed to the DOM. It is where side effects belong: data fetching, subscriptions, timers, logging and manual DOM work that React does not manage.


What is useEffect Hook in React?

useEffect runs code after React has rendered and committed to the DOM. It is where side effects belong: data fetching, subscriptions, timers, logging and manual DOM work that React does not manage.

Why useEffect Hook matters

Rendering must be pure — the same props and state must always produce the same output. Effects give you a safe place to reach outside React without breaking that guarantee.

useEffect Hook example

Effect with dependencies and cleanup
JSX
import { useState, useEffect } from 'react';

function JobDetail({ id }) {
  const [job, setJob] = useState(null);

  useEffect(() => {
    const controller = new AbortController();

    fetch(`/api/jobs/${id}`, { signal: controller.signal })
      .then(r => r.json())
      .then(setJob)
      .catch(err => { if (err.name !== 'AbortError') console.error(err); });

    return () => controller.abort();   // cleanup
  }, [id]);                            // re-run when id changes

  return job ? <h1>{job.title}</h1> : <p>Loading…</p>;
}

How this works

The effect runs after the first render and again whenever id changes. Before each re-run — and on unmount — React calls the cleanup, aborting any request still in flight so a stale response can never overwrite fresh data.

What the dependency array controls

FormWhen the effect runs
useEffect(fn)after every render
useEffect(fn, [])once after the first render
useEffect(fn, [a, b])after the first render, and whenever a or b changes
return () => {…}before each re-run and on unmount

Common mistakes with useEffect Hook

  • Omitting the dependency array and creating an infinite loop when the effect sets state.
  • Listing an object or array dependency that is recreated every render, so the effect never stops firing.
  • Forgetting cleanup for intervals, event listeners and subscriptions.
  • Using an effect to compute a value from existing state — just compute it during render.
💡

If an effect only derives one value from another, delete it. Effects are for synchronising with systems outside React, not for keeping state in sync with itself.

React useEffect Hook— Interview Questions & FAQs

Why does my useEffect run in an infinite loop?+

The effect updates state that is also in its dependency array, or the array is missing entirely. Either narrow the dependencies to primitives, or use a functional state update so the effect does not depend on the value it sets.

Why does useEffect run twice on mount?+

React StrictMode in development mounts, unmounts and remounts each component to check your cleanup works. Add proper cleanup and the second run becomes harmless. Production runs it once.

Related React Topics

Keep learning with these closely related lessons.

Ready to use your React skills?

Find verified React internships and fresher developer jobs across India.

Browse React Internships