Hooks
Say a component needs to remember whether a dropdown is open, or needs to run some code after it renders to sync with a server. Both are jobs for a hook. You've already used two of them: useState gives a component memory, and useEffect runs code after a render to reach outside React. Those are the two you'll use most, and the rest of the built-ins follow the same shape: you call a function inside your component, and it wires you into part of React's machinery. The name of that function always starts with use, and that prefix is the whole definition of a hook.
A tour of the common built-ins
You don't need every hook on day one, but it helps to know what's on the shelf so you recognize the right tool when a problem calls for it. Here are the ones that come up most:
useRefholds a mutable value that survives renders without causing one. It's also how you get a direct reference to a DOM element when you need one, like focusing an input.useContextreads shared data from a surrounding provider, so a deeply nested component can reach a value without you passing it down through every level in between.useReducermanages state through a reducer function, where each update describes a transition from the current state to the next. It's a good fit when state changes get complex enough that a singleuseStatestarts to strain.useMemocaches a computed value between renders and only recalculates it when its inputs change.useCallbackdoes the same for a function, handing you back the same function instance between renders instead of a fresh one each time.
useMemo and useCallback get more room later, once there's a real performance story to tell. Refs and the DOM picks up useRef in full. For useContext and useReducer, this chapter is the deepest treatment they get, so come back here when you need a refresher. For now, the point is the pattern: a hook is your way into a specific React feature, and its name tells you it's a hook.
The rules of hooks
Hooks come with two rules, and React counts on you following them.
First, call hooks at the top level of your component. Don't call them inside a condition, a loop, or a nested function. Every hook a component uses should run on every render, in the same order, every time.
function Profile({ isLoggedIn }) {
// Wrong: this hook only runs sometimes
if (isLoggedIn) {
const [name, setName] = useState('')
}
// Right: call it at the top level, unconditionally
const [name, setName] = useState('')
// ...then use isLoggedIn inside your logic
}Second, only call hooks from React function components or from other custom hooks. Regular JavaScript functions don't get to use hooks, because there's no component render for React to attach the state to.
Writing your own hooks
Say several components need to know the width of the browser window. Instead of copying the same useState and useEffect wiring into each one, you can package that logic once:
import { useState, useEffect } from 'react'
function useWindowWidth() {
const [width, setWidth] = useState(window.innerWidth)
useEffect(() => {
const handleResize = () => setWidth(window.innerWidth)
window.addEventListener('resize', handleResize)
return () => window.removeEventListener('resize', handleResize)
}, [])
return width
}Now any component can call const width = useWindowWidth() and get a value that updates as the window resizes. useWindowWidth is a custom hook: a function that calls other hooks. That's the whole pattern, and because it's a function, you can give a piece of stateful logic a name, put it in one place, and reuse it across components. The use prefix is what marks it as a hook, both for React's tooling and for the next person reading your code. Custom hooks bundle the hooks you already have into something reusable; they don't add new powers to React.
use, and it plugs your component into a React feature like memory or effects. You've already met useState and useEffect, and the others follow the same shape. The main thing to remember: call hooks at the top of your component, never inside an if or a loop, so they run the same way every render. Next up: Thinking in React, where components, props, state, and effects come together into a process for turning a design into a UI.

