Skip to main content

The Hidden Linked List Behind Every React Hook

Md Sajid Hasan
Md Sajid Hasan
Published 11 November 2026·15 min read
Share Article:
The Hidden Linked List Behind Every React Hook

Why you cannot put useState inside an if statement, and what is actually happening inside React when you don't

Every React developer memorizes the rule before understanding it: hooks must be called in the same order, every render, no exceptions. It is baked into the linter, it is the first thing you learn, and most of us just accept it like a tax form. We follow the rule without asking who wrote it or why.

But this rule is not just a style guideline. It is not there to keep your code tidy. It exists because of one specific, very elegant piece of internal engineering: React tracks every hook in your component using nothing more than a linked list. The position of a hook in that list is the only thing that tells React which piece of state belongs to which useState call.

Once you see that, the rule stops feeling random. It becomes obvious. So let us actually go there, into the data structures, into the Fiber architecture, into what happens the exact millisecond you call setState.

Hooks don't have names, they have positions

Here is the detail that changes everything. When you write this:

const [name, setName] = useState("Sajid");
const [age, setAge] = useState(28);

React never sees the words "name" or "age". Those are just variable names in your JavaScript scope, and they vanish the moment the function returns. What React actually receives is two back to back calls to the same function, useState, with no identifying information attached to either call except the order in which they happened.

So how does React know that the second call to useState on this render matches the second call on the previous render, and not the first one, or a brand new one? It cannot ask for a name because there isn't one. Instead, it counts. Literally, it walks a list, one step per hook call, and whichever node it lands on is, by definition, "this hook".

You can think of it like this small simulation of how React tracks hook positions:

// A tiny simplified simulation of "positional" hook tracking
let hookList = [];
let hookIndex = 0;

function useStateSimple(initialValue) {
  const currentIndex = hookIndex;

  if (hookList[currentIndex] === undefined) {
    hookList[currentIndex] = initialValue; // first render, create it
  }

  function setState(newValue) {
    hookList[currentIndex] = newValue;
  }

  hookIndex++;
  return [hookList[currentIndex], setState];
}

function renderComponent() {
  hookIndex = 0; // reset position counter at start of every render
  const [name] = useStateSimple("Sajid");
  const [age] = useStateSimple(28);
  console.log(name, age);
}

Notice hookList[currentIndex] has no idea that this value is called "name". It only knows its position, 0 or 1. This is the whole secret. Identity, in React's hooks system, is positional. It is less like a dictionary lookup and more like reading down a numbered list and trusting that item number 2 is always item number 2.

The moment you wrap a hook call in a condition, like this:

// Don't do this
if (isLoggedIn) {
  useEffect(() => {
    trackLogin();
  }, []);
}

you risk turning what was item number 2 last render into item number 3 this render. React has no way to notice the swap. It just hands your effect the state meant for something else entirely, silently, with no error. That silent corruption is exactly what the Rule of Hooks is meant to prevent. The ESLint plugin exists because React itself cannot check this at runtime without paying a performance cost it is not willing to pay.

The list lives inside something called a Fiber#

To understand where this list actually lives, we need to talk about Fiber, the internal architecture React has used since React 16.

Before Fiber, React's rendering was a simple, uninterruptible recursive walk down your component tree. Once it started rendering, it could not stop until it finished. This became a real problem as apps grew large enough that a single render could visibly block the browser from responding to a keystroke or an animation frame.

Fiber was the rewrite that fixed this. Instead of representing your component tree as a stack of nested function calls, React represe4nts it as a tree of plain JavaScript objects, one Fiber node per component or DOM element, linked together with three pointers: a pointer to its first child, a pointer to its next sibling, and a pointer back to its parent. A simplified Fiber node looks something like this:

// Simplified shape of a Fiber node
const fiberNode = {
  type: "Profile",        // which component this represents
  child: null,            // pointer to first child fiber
  sibling: null,          // pointer to next sibling fiber
  return: null,           // pointer back to parent fiber
  memoizedState: null,    // points to the first Hook node (for function components)
  alternate: null,        // pointer to the matching fiber in the other tree
};

This sounds like a small implementation detail, but it is what allows React to pause in the middle of rendering your app, hand control back to the browser so it can paint a frame or respond to a click, then pick up exactly where it left off. A recursive call stack cannot do that. A linked structure of objects can.

Every Fiber node carries information about the component it represents: what type of component it is, what DOM node or class instance it is connected to, and, this is the part we care about, a field called memoizedState. For a class component, this field holds this.state. For a function component using hooks, it holds something more interesting: a pointer to the very first Hook object in that component's linked list.

So the picture looks like this. Your component tree is one linked structure, made of Fiber nodes. Hanging off each individual function-component Fiber is a second, completely separate linked list, a private ordered sequence of every hook that component called, in the order it called them. useState here, useEffect there, useRef after that, each one gets its own node, and each node points to the next.

This is why hook order matters specifically within a component and nowhere else. Each component's hook list is its own independent chain. React is not comparing your hooks to some global registry. It is comparing this render's chain, node by node, to last render's chain for that exact same Fiber.

If you strip away all the surrounding machinery, the object React creates for each hook call looks roughly like this:

// One node in a component's hook linked list
const hookNode = {
  memoizedState: null, // the actual value: state, ref, memoized result...
  queue: null,          // pending updates waiting to be applied
  next: null,           // pointer to the NEXT hook called in this component
};

//A component that calls three hooks ends up with three of these, chained together purely by call order:
// After Profile() calls useState, useState, then useEffect:
FiberNode.memoizedState = {
  memoizedState: "Sajid",
  queue: null,
  next: {
    memoizedState: 28,
    queue: null,
    next: {
      memoizedState: /* effect data */ null,
      queue: null,
      next: null,
    },
  },
};

Nothing in that structure records the name name, age, or anything else from your source code. These are just three anonymous boxes, each pointing to the next, in the order they were created.

Two trees, not one#

There is a second piece of Fiber design worth knowing, because it explains how updates happen without disturbing what is currently on screen.

React actually keeps two copies of your Fiber tree in memory at any given moment: the tree that is currently rendered to the DOM, called current, and a second, work-in-progress tree that React builds when a new render is triggered. Each Fiber in the work-in-progress tree keeps a direct pointer to its counterpart in the current tree, called alternate, which lets React reuse and compare data between the two without recomputing everything from scratch.

// Roughly how the two trees relate
currentFiber.alternate = workInProgressFiber;
workInProgressFiber.alternate = currentFiber;

This "double buffering" is what makes an interrupted, resumed, or even abandoned render completely safe. Nothing on screen changes until the finished work-in-progress tree is committed and swapped in as the new current tree. It is the same idea video games use for rendering frames without tearing, applied here to UI reconciliation.

This is also, not by coincidence, exactly how React knows whether a component is mounting for the first time or updating. If a Fiber has no alternate, there is no previous render to compare against, so this is a first render, a mount. If it does have an alternate, there is an existing hook list sitting on the current tree waiting to be walked and reused, so this is an update. That one distinction quietly changes which internal implementation your hook calls get routed to.

Mounting builds the list, updating walks it#

This is the part people find most surprising: useState is not one function internally. It is actually two functions, and React switches between them by swapping out something called the current dispatcher before your component function even runs.

The switch itself is almost anticlimactic in how simple it is. Before your component function runs, React does something like this:

// Simplified from React's internals
ReactCurrentDispatcher.current = isFirstRender
  ? HooksDispatcherOnMount   // creates new hook nodes
  : HooksDispatcherOnUpdate; // walks and clones existing hook nodes

Every hook you call, useState, useRef, useEffect, is really just looking up whichever function the current dispatcher has registered under that name. Call useState during a mount and you get one function. Call it during an update and you silently get a completely different function, even though your code looks identical in both cases.

On a component's first render, React points the dispatcher at the "mount" versions of every hook. Each hook call in this mode does the same basic thing: it creates a brand new node, a small object to hold this hook's state and its queue of pending updates, and appends that node to the end of the Fiber's hook list. The first hook call becomes the head of the list, and every call after that gets chained on as the tail.

On every render after the first, React points the dispatcher at the "update" versions instead. These do something quietly different. They do not create anything new. They reach into the current, already committed Fiber's hook list, find the next node in line, and clone it onto the new work-in-progress Fiber, carrying forward its stored state, and applying any pending updates that have queued up since the last render.

In rough pseudocode, the two versions look like this:

function mountState(initial) {
  const hook = { memoizedState: initial, queue: [], next: null };
  appendToList(hook);       // brand new node, added to the tail
  return [hook.memoizedState, dispatch];
}

function updateState() {
  const hook = getNextHookFromCurrentTree(); // walk, don't create
  hook.queue.forEach(update => {
    hook.memoizedState = update(hook.memoizedState);
  });
  hook.queue = [];
  return [hook.memoizedState, dispatch];
}

Notice that updateState never asks "which hook am I?". It just asks for whatever node comes next in the list, the same operation, no matter whether that node represents state, a ref, or anything else.

That second function has no idea whether it is being called for useState, useRef, or any other hook, and no idea what your variable is called. All it does is take one more step down a list and say, "here is the next node, whatever called me can do what it wants with it." The simplicity of that operation is exactly why it is so unforgiving about order.

What actually happens when you call setState#

It is worth walking through what happens the instant a user clicks a button and your click handler calls setCount(count + 1).

Nothing re-renders immediately. What actually happens first is much smaller: React finds the specific Hook node your setCount closure is tied to, and appends a tiny "update" record, basically just the new value or the function you passed in, onto that node's own private queue. Then it walks up the Fiber tree from that component to the root, marking every ancestor along the way as having pending work to do, and hands the job off to React's scheduler to decide when it is worth actually doing that work.

This is also the mechanism behind React batching multiple state updates together. If you call three different setters inside the same click handler, you are not triggering three renders. You are appending three update records onto (possibly three different) queues, and the very next time each hook's node gets visited during a render, all the updates sitting in its queue get folded together into one single final value, in one pass, before your component function ever runs again.

When the render does happen, that hook's node is walked exactly the way described above: pulled from the current tree, cloned, and this time its queue is not empty. React applies each pending update in order, arrives at a final value, and that is what your useState call returns this time.

You can see the batching behaviour directly if you picture what the queue looks like after a handler calls the same setter multiple times in a row:

function handleClick() {
  setCount(c => c + 1); // pushes an update onto the queue
  setCount(c => c + 1); // pushes a second update onto the same queue
  setCount(c => c + 1); // pushes a third
}

// Internally, that one hook's queue now looks like this:
// queue = [c => c + 1, c => c + 1, c => c + 1]

// On the NEXT render, React folds all three into one final value
// in a single pass, before your component function runs again:
// 0 -> 1 -> 2 -> 3

Compare that with the "stale closure" version, which is a very common mistake:

function handleClickWrong() {
  setCount(count + 1); // count is 0, so this pushes "set to 1"
  setCount(count + 1); // count is STILL 0 here, so this also pushes "set to 1"
  setCount(count + 1); // same thing again, "set to 1"
}

// queue = [1, 1, 1]
// Final result: count becomes 1, not 3

This is exactly why setCount(count + 1) repeated three times in the same handler does not triple the count. Each call captures the same stale count from the closure, so the queue ends up holding three identical "replace with 1" instructions instead of three increments. Using the updater-function form, c => c + 1, is what lets each queued update build on the result of the one before it.

Why this matters beyond trivia#

It is easy to read all this and file it away as "interesting internals trivia", but it has real, everyday consequences for how you write components.

Custom hooks are not magic wrappers, they are just more list nodes. When your component calls a custom hook like useDebouncedValue(), and that hook internally calls useState and useEffect, those calls do not happen in some isolated bubble. They get spliced directly into your component's hook list, at exactly the point where you called the custom hook. This is why the ordering rule applies just as strictly through layers of custom hooks as it does to hooks called directly. A conditionally invoked custom hook is just as dangerous as a conditionally invoked useState.

// This custom hook's internal hooks get spliced
// into whatever component calls it, at that exact position
function useDebouncedValue(value, delay) {
  const [debounced, setDebounced] = useState(value); // one list node
  useEffect(() => {                                   // another list node
    const timer = setTimeout(() => setDebounced(value), delay);
    return () => clearTimeout(timer);
  }, [value, delay]);
  return debounced;
}

Early returns are the most common way this rule gets broken by accident. A pattern like "return null early if there is no data, then use a hook further down" looks perfectly innocent until the component re-renders with data present, and suddenly a hook that did not run before is running now, at a position in the list that used to belong to something else.

// Risky pattern, breaks hook order between renders
function Profile({ data }) {
  if (!data) {
    return null; // early return before all hooks have run
  }

  const [isEditing, setIsEditing] = useState(false); // sometimes runs, sometimes not
  return <div>{data.name}</div>;
}

// Safer pattern, hooks always run in the same order
function Profile({ data }) {
  const [isEditing, setIsEditing] = useState(false); // always runs first

  if (!data) {
    return null; // early return happens AFTER hooks are declared
  }

  return <div>{data.name}</div>;
}

There is a small but real cost to every hook you add. Because each hook is a node that has to be created on mount and walked, cloned, and updated on every render after that, a component with fifteen hooks does meaningfully more bookkeeping per render than one with two. It is rarely something to obsess over, but it is a useful thing to keep in mind when a shared component in a design system starts accumulating hooks.

The takeaway#

Everything in this article comes down to one idea, repeated at different layers of the system: React identifies hooks by where they sit in a list, not by what they are called or what they do. That list is built fresh on a component's first render, then re-walked, node by node, on every render after that, and this only works if the calls that produced it happen in the same order every single time.

Fiber is the surrounding architecture that makes this efficient and interruptible, a tree of linked nodes standing in for your component tree, doubled up so that an in-progress render never disturbs what is already on screen. But the hooks system itself, the part that trips people up, really is just a linked list, walked in order, over and over, for as long as your component is alive.

Once you see it that way, the rule you memorized on day one stops being a random constraint and starts looking like exactly what it is: the natural result of a genuinely clever design.

Found this useful? Share it with your peers:

More Blogs & Related Reads

Continue reading technical insights written by Exabyting engineers