What are React hooks, and why were they added?

BeginnerReact interview questionBy , updated 17 November 2026

Short answer

Hooks are functions whose names start with use, such as useState and useEffect, that let function components hold state, run side effects and read context. They arrived in React 16.8 to fix three problems with class components: stateful logic was hard to reuse, related code was split across lifecycle methods, and classes brought this binding confusion. Custom hooks made logic shareable as plain functions.

What a hook looks like

import { useEffect, useState } from "react";

function OnlineBadge() {
  const [online, setOnline] = useState(navigator.onLine);

  useEffect(() => {
    const update = () => setOnline(navigator.onLine);
    window.addEventListener("online", update);
    window.addEventListener("offline", update);
    return () => {
      window.removeEventListener("online", update);
      window.removeEventListener("offline", update);
    };
  }, []);

  return <span>{online ? "Online" : "Offline"}</span>;
}

The component is still a plain function that returns JSX. useState gives it a value that survives re-renders, and useEffect subscribes to the browser and cleans up when the component unmounts. Before hooks, this needed a class with a constructor, componentDidMount and componentWillUnmount.

The problems with classes that hooks solved

Reusing stateful logic was awkward. Classes had no clean way to share behaviour such as "track window size" or "subscribe to a store". The patterns people used, higher-order components and render props, worked but wrapped components in layers of extra components ("wrapper hell") and hid where props came from. A custom hook is just a function that calls other hooks, so const size = useWindowSize() shares the logic with no extra components in the tree.

Related code was split by lifecycle. In a class, setting up a subscription went in componentDidMount, updating it in componentDidUpdate and removing it in componentWillUnmount. One feature was spread over three methods, and each method mixed several unrelated features. An effect keeps the setup and its cleanup together, and you can have one effect per concern. How do React class lifecycle methods map to hooks? shows the translation.

Classes were harder to understand and to optimise. You had to bind event handlers, remember what this pointed to, and reason about this.props changing under an async callback. The React team also said classes made some compiler optimisations harder. Function components with hooks read top to bottom and capture the props of the render they belong to.

Hooks did not remove classes. Class components still work in React 19, and error boundaries still need one. New code uses function components and hooks almost everywhere; see Class components vs function components in React.

The main built-in hooks

Hook What it does
useState Holds a value that persists between renders; setting it triggers a re-render
useReducer Like useState, but updates go through a reducer function; good for related fields
useContext Reads the nearest value from a context provider
useRef Holds a mutable value that does not trigger re-renders; also points at DOM nodes
useEffect Runs side effects after the browser paints, with optional cleanup
useLayoutEffect Like useEffect, but runs before paint, for measuring layout
useMemo Caches the result of a calculation between renders
useCallback Caches a function so its identity stays stable
useTransition Marks an update as non-urgent so the UI stays responsive
useDeferredValue Lets a slow part of the UI lag behind a fast-changing value
useId Generates a stable unique id, safe for server rendering
useSyncExternalStore Subscribes to a store outside React without tearing
useImperativeHandle Chooses what a parent sees through a ref
useActionState React 19: tracks the result and pending state of a form action
useOptimistic React 19: shows an optimistic value while an async action runs

React 19 also added use, which reads a promise or a context. It is not quite a hook: unlike the others, it can be called inside an if.

Each of these has depth this page does not repeat. Start with How does useEffect work?, useState vs useReducer and useMemo vs useCallback.

The rules that come with hooks

Hooks rely on call order. React stores each component's hook values in a list and matches them up by position on every render, so hooks must be called at the top level of a component or custom hook, never inside conditions, loops or nested functions. What are the rules of hooks in React? explains why breaking that order mixes up state, and the lint rule that catches it.

Practise it

Common follow-up questions

What is a custom hook?

A function whose name starts with use and that calls other hooks. It shares logic, not state: each component that calls it gets its own copy. See What are custom hooks in React?.

Do hooks make class components obsolete?

For new code, mostly. Classes are still supported and will keep working. The one thing hooks cannot do yet is catch render errors, so error boundaries are still classes or come from a small library.

Why must hook names start with use?

The name is how React's lint rules recognise a hook and check that it follows the rules. A function that does not call hooks should not use the prefix.

Are hooks slower than classes?

Not in any way that usually matters. Creating closures on each render is cheap. Real performance problems come from unnecessary re-renders and heavy work during render, which are the same with classes.

Mistakes to avoid

  • Calling a hook inside an if, a loop or an event handler

  • Treating useEffect as a lifecycle method and putting unrelated work in one effect

  • Using an effect to compute values that could be derived during render

  • Naming an ordinary helper useSomething when it calls no hooks

  • Reaching for useMemo and useCallback everywhere before measuring

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Convert a class component with lifecycle methods to hooksMedium35 min
  2. Refactor a withAuth HOC and a render-prop loader into hooks without changing behaviourMedium40 min
  3. Implement a mini useState and useEffect hooks engineHard60 minFree
  4. Fix components and custom hooks that break the rules of hooksMedium35 min
  5. Build everyday custom hooks: useToggle, useWindowSize, useOnlineStatus and useCopyToClipboardMedium35 min