We’ve all heard about it. The new hook system of React 16.7 has made a lot of noise in the community. We’ve all tried it and tested it, and got really excited about it and its potential. When you think about hooks they’re kind of magical, somehow React manages your component without even exposing its instance (no use of this keyword). So how the heck does React does that?
Today I would like to dive into React’s implementation of hooks so we can understand it better. The problem with magical features is that it’s harder to debug a problem once it happens, because it’s backed by a complex stack trace. Thus, by having a deep knowledge regards React’s new hook system, we would be able to solve issues fairly quick once we encounter them, or even avoid them from in the first place.
Before I begin I would just like to say that I’m not a developer/maintainer of React and that my words should be taken with a grain of salt. I did dive very deeply into the implementation of React’s hooks system, but by all means I can’t guarantee that this is how React actually works. With that said, I’ve backed my words with proofs and references from React’s source code, and tried to make my arguments as solid as possible.
First of all, let’s go through the mechanism that ensures that hooks are called within React’s scope, because you’d probably know by now that hooks are meaningless if not called in the right context:
The dispatcher is the shared object that contains the hook functions. It will be dynamically allocated or cleaned up based on the rendering phase of ReactDOM, and it will ensure that the user doesn’t access hooks outside a React component (see implementation).
The hooks are enabled/disabled by a flag called enableHooks right before we render the root component by simply switching to the right dispatcher; this means that technically we can enable/disable hooks at runtime. React 16.6.X also has the experimental feature implemented, but it’s actually disabled (see implementation).
When we’re done performing the rendering work, we nullify the dispatcher and thus preventing hooks from being accidentally used outside ReactDOM’s rendering cycle. This is a mechanism that will ensure that the user doesn’t do silly things (see implementation).
The dispatcher is resolved in each and every hook call using a function called resolveDispatcher(). Like I said earlier, outside the rendering cycle of React this should be meaningless, and React should print the warning message: “Hooks can only be called inside the body of a function component” (see implementation).
Now that we got that simple encapsulation mechanism covered, I would like us to move to the core of this article — the hooks. Right of the bet I’d like to introduce you to a new concept:
Behind the scenes, hooks are represented as nodes which are linked together in their calling order. They’re represented like so because hooks are not simply created and then left alone. They have a mechanism which allows them to be what they are. A hook has several properties which I would like you to bare in mind before diving into its implementation:
Its initial state is created in the initial render.
Its state can be updated on the fly.
React would remember the hook’s state in future renders.
React would provide you with the right state based on the calling order.
React would know which fiber does this hook belong to.
Accordingly, we need to rethink the way we view the a component’s state. So far we have thought about it as if it’s a plain object:
But when dealing with hooks it should be viewed as a queue, where each node represents a single model of the state:
The schema of a single hook node can be viewed in the implementation. You’ll see that the hook has some additional properties, but the key for understanding how hooks work lies within memoizedState and next. The rest of the properties are used specifically by the useReducer() hook to cache dispatched actions and base states so the reduction process can be repeated as a fallback in various cases:
baseState - The state object that would be given to the reducer.
baseUpdate - The most recent dispatched action that created the baseState.
queue - A queue of dispatched actions, waiting to go through the reducer.
Unfortunately I haven’t managed to get a good grasp around the reducer hook because I didn’t manage to reproduce almost any of its edge cases, so I wouldn’t feel comfortable to elaborate. I will only say that the reducer implementation is so inconsistent that even one of the comments in the implementation itself states that “(it’s) not sure if these are the desired semantics”; so how am I supposed to be sure?!
So back to hooks, before each and every function Component invocation, a function named prepareHooks() is gonna be called, where the current fiber and its first hook node in the hooks queue are gonna be stored in global variables. This way, any time we call a hook function (useXXX()) it would know in which context to run.
Once an update has finished, a function named finishHooks() will be called, where a reference for the first node in the hooks queue will be stored on the rendered fiber in the memoizedState property. This means that the hooks queue and their state can be addressed externally:
Let’s get more specific and talk about individual hooks, starting with the most common of all — the state hook:
You would be surprised to know, but behind the scenes the useState hook uses useReducer and it simply provides it with a pre-defined reducer handler (see implementation). This means that the results returned by useState are actually a reducer state, and an action dispatcher. I would like you to take a look at the reducer handler that the state hook uses:
So as expected, we can provide the action dispatcher with the new state directly; but would you look at that?! We can also provide the dispatcher with an action function that will receive the old state and return the new one. This isn’t documented anywhere in the official React documentation (as for the time this article was written) and I’m actually quiet happy about it. This means that when you send the state setter down the component tree you can run mutations against the current state of the parent component, without passing it as a different prop. For example:
Lastly, effect hooks — which made a major impact on a component’s life cycle and how it works:
Effect hooks behave slightly differently and has an additional layer of logic that I would like to explain. Again, there are things I would like you to bare in mind regards the properties of the effect hooks before I dive into the implementation:
They’re created during render time, but they run after painting.
If given so, they’ll be destroyed right before the next painting.
They’re called in their definition order.
Note that I’m using the “painting” term and not “rendering”. These two are different things, and I’ve seen many speakers in the recent React Conf use the wrong term! Even in the official React docs they say “after the render is committed to the screen”, which is kind of like “painting”. The render method just creates the fiber node but doesn’t paint anything yet.
Accordingly, there should be another an additional queue that should hold these effects and should be addressed after painting. Generally speaking, a fiber holds a queue which contains effect nodes. Each effect is of a different type and should be addressed at its appropriate phase:
Invoke instances of getSnapshotBeforeUpdate() before mutation (see implementation).
Perform all the host insertions, updates, deletions and ref unmounts (see implementation).
Perform all life-cycles and ref callbacks. Life-cycles happen as a separate pass so that all placements, updates, and deletions in the entire tree have already been invoked. This pass also triggers any renderer-specific initial effects (see implementation).
Effects which were scheduled by the useEffect() hook - which are also known as “passive effects” based on the implementation (maybe we should start using this term within the React community?!).
When it comes to the hook effects, they should be stored on the fiber in a property called updateQueue, and each effect node should have the following schema (see implementation):
tag - A binary number which will dictate the behavior of the effect (I will elaborate soon).
create - The callback that should be ran after painting.
destroy - The callback returned from create() that should be ran before the initial render.
inputs - A set of values that will determine whether the effect should be destroyed and recreated.
next - A reference to the next effect which was defined in the function Component.
Besides the tag property, the other properties are pretty straight forward and easy to understand. If you’ve studied hooks well, you’d know that React provides you with a couple of special effect hooks: useMutationEffect() and useLayoutEffect(). These two effects internally use useEffect(), which essentially mean that they create an effect node, but they do so using a different tag value.
The tag is composed out of a combination of binary values (see implementation):
The most common use cases for these binary values would be using a pipeline (|) and add the bits as is to a single value. Then we can check whether a tag implements a certain behavior or not using an ampersand (&). If the result is non-zero, it means that the tag implements the specified behavior.
Here are the supported hook effect types by React along with their tags (see implementation):