πŸͺ

React Hooks

Intermediate β†’ Advanced

All 15+ React hooks explained with interactive demos and real-world patterns

Rules of Hooks

1. Only call hooks at the top level β€” not inside loops, conditions, or nested functions. 2. Only call hooks from React functions β€” not regular JS functions. ESLint plugin eslint-plugin-react-hooks enforces these.

useState β€” Local Component State

beginner

Stores a value between renders. Returns [state, setter]. Setter triggers re-render. Use functional updates when new state depends on previous.

useEffect β€” Side Effects

beginner

Synchronize with external systems (APIs, timers, event listeners, localStorage). Runs after render. Return a cleanup function to avoid memory leaks.

useRef β€” DOM Refs & Mutable Values

intermediate

Two use cases: 1) Access DOM elements imperatively. 2) Store mutable values that don't trigger re-renders (like timers, previous values, render counts).

useMemo β€” Memoize Expensive Computations

intermediate

Cache the result of an expensive calculation between renders. Only recomputes when dependencies change. Don't overuse β€” has its own cost (memory + comparison).

useCallback β€” Stable Function References

intermediate

Memoize a function between renders. Returns same function reference if deps don't change. Useful when passing callbacks to memoized child components.

useReducer β€” Complex State Logic

intermediate

useState on steroids. Manages complex state transitions with a reducer function. Great when next state depends on current, or when you have multiple related state variables.

Custom Hooks β€” Reusable Logic

intermediate

Extract stateful logic into reusable functions starting with 'use'. Custom hooks can call other hooks and are the primary way to share stateful logic in React.

useTransition & useDeferredValue β€” Concurrent React

advanced

Mark state updates as non-urgent so React can prioritize more critical updates (like input responsiveness). Part of React 18 Concurrent Mode.

Other Hooks β€” useId, useLayoutEffect, useImperativeHandle

advanced

Less common but important hooks for specific use cases: accessibility IDs, layout measurements, and exposing imperative APIs from components.