useEffect
The React hook for running side work, such as adding listeners or changing document.body, after a component renders.
In more detail
An effect can return a cleanup function that React runs before the effect runs again and when the component unmounts. Overlays use it to lock scroll and set inert, and the cleanup restores the previous values.
Example
useEffect(() => { const id = setInterval(tick, 1000); return () => clearInterval(id); }, []);Related terms
- React hook — A function whose name starts with use, such as useState or useEffect, that lets a React component keep state or run side work.
- mount and unmount — Mounting is when a component is first added to the page; unmounting is when it is removed.
- scroll locking — Stopping the page behind an overlay from scrolling, usually by setting overflow: hidden on the body while the overlay is open.
- cleanup function — The function an Effect returns so React can undo what the setup did, such as removing a listener or cancelling a request.
- data fetching — Loading the data a page needs, such as products or user details, from a database or API.
- template.tsx — A special file like layout.tsx that wraps its children but is created fresh on every navigation, so its state resets.
- dependency array — The array of values given to hooks such as useEffect, useMemo and useCallback that tells React when to re-run them.
- external system — Anything outside React that a component must stay in step with, such as the network, a timer, a browser API, a chat server or a non-React library.
- useLayoutEffect — A version of useEffect that runs synchronously after React changes the DOM but before the browser paints the screen.
- derived state — A value that can be calculated from existing props or state, so it should be computed during render instead of stored as separate state.
Taught in
- Overlay Architecture: Portals, Stacking Contexts & Backdrops — Design Systems & Component Engineering
- Focus Management: Trapping, Restoration & Focus Rings — Design Systems & Component Engineering
- Server Components: Async Execution & Constraints — Next.js 15 & Modern App Router Architecture
- Templates vs Layouts: Remounting & Page Transitions — Next.js 15 & Modern App Router Architecture
- Async Data Fetching: Parallel Requests & Preloading — Next.js 15 & Modern App Router Architecture
- Action State: useActionState & Optimistic Form UI — Next.js 15 & Modern App Router Architecture
- Component Purity: Pure Functions, Idempotence And Side Effects — React 19 & Modern Component Architecture
- Complex State with useReducer: State Machines And Action Types — React 19 & Modern Component Architecture
- Virtual DOM vs Real DOM: The Reconciliation Cost Model — React 19 & Modern Component Architecture
- Keys In Depth: Identity Preservation And Identity Resetting — React 19 & Modern Component Architecture
- Diffing Algorithm: Element Type Shifts And Subtree Tear Down — React 19 & Modern Component Architecture
- SyntheticEvent System: Event Delegation in Modern React — React 19 & Modern Component Architecture
- useEffect Lifecycle: Mount, Dependencies, And Cleanup Phases — React 19 & Modern Component Architecture
- Avoiding Effect Antipatterns: Derived State And Events — React 19 & Modern Component Architecture
- useLayoutEffect vs useEffect: Painting And Synchronous Reads — React 19 & Modern Component Architecture
- useRef Mechanics: DOM References And Mutable Persistent State — React 19 & Modern Component Architecture