What is a linked list — and why a React hook can't hide inside an if

AI wrote you a component. You added one line: if (!user) return null — near the top, before useState. And React crashed with "Rendered fewer hooks than expected".
Odd, right? You didn't break anything, you just left early. But React doesn't store hooks by name — it stores them as a chain, in call order. That chain is a linked list. Let's figure out what it is, and the error will suddenly make sense.
What is a linked list
A linked list is a chain of items where each item stores its value and a reference to the next one. That's it. No numbers. No index.
Compare it with an array. An array is a row of numbered boxes: want the fifth — grab arr[4] instantly. A linked list is a scavenger hunt. The first note tells you where the second is. The second tells you where the third is. To reach the fifth, you walk past four.
const third = { value: 'tea', next: null }
const second = { value: 'bread', next: third }
const first = { value: 'milk', next: second }
let node = first
while (node) {
console.log(node.value)
node = node.next
}
// milk, bread, tea
See: no index anywhere. Just "here I am, and here's the next one".
Why bother, if arrays exist
Arrays seem handier. But a chain has one strength: inserting or removing an item in the middle is cheap.
In an array, inserting at the start means every other item shifts by one. A thousand items — a thousand moves. In a chain you rewrite a single reference: "the newcomer goes after me now". Nobody else even notices.
The price is lookup. Finding an item by number in an array is instant. In a chain, you walk from the start.
In short:
- Array — fast to read by number, slow to insert at the start or middle.
- Linked list — fast to insert and remove, slow to search.
JavaScript has no built-in linked list. You'll almost never write one by hand. But you use one every day — you just don't know it.
Where it hides: React hooks
Back to the error. Inside React, every component has a chain of hooks. The first useState is link one, the second is link two, useEffect is link three. When the component re-renders, React walks the chain and hands out values in order: "first hook gets its state, second gets its own".
The links have no names. React doesn't know link one is count. It only knows it's first.
Now imagine: last time three hooks ran, this time you returned early — and only one ran. The chain broke. Or a hook inside an if: present in one render, missing in the next. Then the second hook receives the third one's state — and your counter suddenly shows text.
React catches this and complains: "Rendered fewer hooks than expected" or "change in the order of Hooks". Hence the main rule of hooks: always call them, in the same order, at the top of the component. Not inside an if, not in a loop, not after an early return.
Fixing the example from the start: move if (!user) return null below all the hooks. All the useState and useEffect calls first, checks after.
Where else: Git history
The second place is Git. Every commit stores a reference to its parent — the previous commit. There's no reference to the "next" one.
That's why git log walks backwards so easily: take a commit, look at its parent, jump, repeat. That's traversing a linked list. But "which commit comes after this one?" Git can't answer with a single reference — it has to scan the history.
Strictly speaking, Git history isn't a simple chain: a merge commit has two parents. But the principle holds: each commit only knows where it came from.
How this helps you
Three practical takeaways:
- See an error about hook order — look for a hook inside an
if, a loop, or after an earlyreturn. That's 90% of cases. - Asking AI to add a condition to a component — add to your prompt: "don't touch the hooks, put the condition after them". Models sometimes break this rule themselves.
- Reading about data structures — remember the pair "array = number, list = reference". Trees and graphs come next, and they're the same idea with references.
Is a linked list faster than an array?
Depends on the operation. Inserting at the start — yes, faster. Reading by number — no, much slower. In everyday JavaScript an array is almost always the better pick.
Do I need to write a linked list myself?
Almost never, unless you're in a job interview. But the idea is worth knowing: it explains the rules of hooks, how Git works, and half of the algorithm puzzles out there.
What is a doubly linked list?
A chain where each item knows both the next and the previous one. It's a common base for caches that evict the least recently used item: you can pull an item out of the middle and move it to the front cheaply.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





