# Hooks, Refs and State Patterns

> useState, useReducer, useRef and custom hooks, plus the rules that keep hooks working correctly. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/frontend/react-hooks
Last updated: 2026-11-28

useState, useReducer, useRef and custom hooks, plus the rules that keep hooks working correctly.

Intermediate · about 6 hours · difficulty 3/5

Learn first: [React](https://www.internhack.in/roadmaps/tracks/frontend/react)

Hooks are how function components remember things and reuse logic. They look like ordinary function calls, but React tracks them **by call order**, which is why the rules of hooks exist.

### The rules

1. Call hooks only at the top level of a component or custom hook, never inside conditions, loops or after an early return.
2. Call them only from React functions. The `eslint-plugin-react-hooks` rules enforce both; keep them on.

### State hooks

- `useState` for independent values. Use the **updater form** `setCount(c => c + 1)` when the next value depends on the previous one.
- `useReducer` when several values change together or transitions have names (`dispatch({ type: "added", item })`). A reducer is a pure function, easy to test.
- Model state carefully: avoid redundant or contradictory state. A `status: "idle" | "loading" | "error"` beats three booleans.

### Refs

`useRef` holds a mutable value that survives renders **without** causing re-renders. Use it for DOM nodes (focusing an input, measuring size), timers and the latest value of something. Do not read or write refs during rendering for things that affect output.

### Custom hooks

Extract reusable stateful logic into functions named `useSomething`: `useDebounce`, `useLocalStorage`, `useMediaQuery`. Each component that calls a custom hook gets its own independent state.

### Good enough to move on

You can refactor tangled `useState` calls into a reducer, focus an input with a ref, and write and reuse two custom hooks of your own.

## Key concepts

- **Rules of hooks**
- **useState updater functions**
- **useReducer**
- **Structuring state to avoid contradictions**
- **useRef for DOM access and mutable values**
- **Custom hooks**
- **useId and other utility hooks**

## Mini project

Build a multi-step checkout form driven by useReducer, with a custom useLocalStorage hook to persist progress and a ref that focuses the first invalid field on submit.

## Check yourself

Why does calling \`setCount(count + 1)\` three times in one click handler only increase count by one?

## Free resources

- [Managing State](https://react.dev/learn/managing-state) (react.dev)
- [Reusing Logic with Custom Hooks](https://react.dev/learn/reusing-logic-with-custom-hooks) (react.dev)
- [Referencing Values with Refs](https://react.dev/learn/referencing-values-with-refs) (react.dev)

Previous: [Svelte](https://www.internhack.in/roadmaps/tracks/frontend/svelte) · Next: [Effects and Synchronisation](https://www.internhack.in/roadmaps/tracks/frontend/react-effects)

## More in React in Depth

- [Hooks, Refs and State Patterns](https://www.internhack.in/roadmaps/tracks/frontend/react-hooks)
- [Effects and Synchronisation](https://www.internhack.in/roadmaps/tracks/frontend/react-effects)
- [Context and Component Composition](https://www.internhack.in/roadmaps/tracks/frontend/react-context)
- [Rendering, Reconciliation and Memoisation](https://www.internhack.in/roadmaps/tracks/frontend/react-rendering)
- [Suspense, Error Boundaries and Concurrent Features](https://www.internhack.in/roadmaps/tracks/frontend/react-suspense)
- [React Server Components and Server Actions](https://www.internhack.in/roadmaps/tracks/frontend/react-server-components)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Frontend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/frontend)
