Skip to content

Hook

In one line

A Hook is a special function that lets you 'hook into' React features like state and lifecycle from a function component.

In simple words

Hooks are functions that let function components use features that were previously only available in class components — state, lifecycle effects, context, refs, and more. They start with the word use: useState, useEffect, useContext, useRef, etc.

Hooks must follow two rules: (1) only call Hooks at the top level of your component (not inside loops, conditions, or nested functions), and (2) only call Hooks from React function components or custom Hooks.

Hooks were introduced in React 16.8 and fundamentally changed how React code is written. They make logic reusable without changing the component hierarchy — you extract shared logic into a custom Hook and call it from any component.

Example

jsx
function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    const id = setInterval(() => setSeconds(s => s + 1), 1000);
    return () => clearInterval(id);
  }, []);

  return <span>{seconds}s</span>;
}

useState and useEffect are Hooks. They let this function component hold state and run a side effect (an interval) on mount.

Common confusions

  • Confused with: lifecycle method

    The difference: Lifecycle methods (componentDidMount, etc.) belong to class components. Hooks (useEffect, etc.) achieve the same in function components with a unified API. You cannot use Hooks in class components.

Related terms

Related Resources

Related Lessons

  • State

    Introduction to the useState Hook.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary