Skip to main content
Hooks are functions that let you “hook into” React state and lifecycle features from function components. They were introduced in React 16.8 to allow function components to have state and side effects without needing to convert to class components.

Why Hooks?

Hooks solve several problems:
  • Reusable stateful logic: Share logic between components without wrapper hell
  • Simpler components: No need for class components and this keyword
  • Better code organization: Group related logic together instead of splitting it across lifecycle methods
  • Smaller bundle sizes: Function components are smaller than classes

Rules of Hooks

Critical: You must follow these rules when using Hooks, or your component will break:
  1. Only call Hooks at the top level: Don’t call Hooks inside loops, conditions, or nested functions
  2. Only call Hooks from React functions: Call them from function components or custom Hooks, not regular JavaScript functions
According to React’s source code in ReactHooks.js, if these rules are violated, you’ll see an error:

Built-in Hooks

React provides several built-in Hooks. Below is a comprehensive overview based on React’s source code.

State Hooks

useState

Adds state to function components:
The initial state can be a value or a function:

useReducer

An alternative to useState for complex state logic:

Effect Hooks

useEffect

Performs side effects in function components:

useLayoutEffect

Like useEffect, but fires synchronously after DOM mutations:

useInsertionEffect

Fires before all DOM mutations, primarily for CSS-in-JS libraries:

Context Hook

useContext

Reads the current value from a context:
According to React’s source code, calling useContext(Context.Consumer) is not supported and will cause bugs. Always call useContext(Context) instead.

Ref Hook

useRef

Creates a mutable ref object that persists across renders:
Refs are also useful for storing mutable values:

Performance Hooks

useMemo

Memoizes a computed value:

useCallback

Memoizes a function:
Only use useMemo and useCallback when you have a proven performance problem. They add memory overhead and complexity, so don’t use them prematurely.

Transition Hooks

useTransition

Marks state updates as non-urgent (transitions):

useDeferredValue

Defers updating a value:

Other Hooks

useId

Generates unique IDs for accessibility attributes:

useSyncExternalStore

Subscribes to an external store:

useImperativeHandle

Customizes the ref value exposed to parent components:

useDebugValue

Displays a label in React DevTools:

Experimental Hooks

These are available in React but may change:

useOptimistic

Shows optimistic state during async operations:

useActionState

Manages state for form actions:

use

Reads the value of a Promise or Context:

Complete Hook List from React Source

Based on ReactHooks.js, here are all available hooks:
  • useState - State management
  • useReducer - Complex state management
  • useRef - Mutable refs
  • useEffect - Side effects
  • useLayoutEffect - Synchronous side effects
  • useInsertionEffect - CSS injection
  • useContext - Read context
  • useCallback - Memoize functions
  • useMemo - Memoize values
  • useImperativeHandle - Customize ref
  • useDebugValue - DevTools label
  • useTransition - Non-urgent updates
  • useDeferredValue - Defer value updates
  • useId - Unique IDs
  • useSyncExternalStore - External store subscription
  • useOptimistic - Optimistic UI
  • useActionState - Form actions
  • use - Read promises/context

Custom Hooks

You can create your own Hooks to reuse stateful logic:
Custom Hooks must start with “use” (like useWindowSize, useFetch, etc). This convention is important because it lets React check for Hook rule violations.

Common Patterns

Fetching Data

Form Handling

Next Steps

Rendering

Learn how React renders and updates the DOM

Lifecycle

Understand component lifecycle in depth