Under the hood of React’s hooks system

Under the hood of React’s hooks system

Under the hood of React’s hooks system: Looking at the implementation and getting to know it inside out

Looking at the implementation and getting to know it inside out

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 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.

A rough schematic representation of React’s hooks system

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

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).

let currentDispatcher
const dispatcherWithoutHooks = { /* ... */ }
const dispatcherWithHooks = { /* ... */ }
function resolveDispatcher() {
if (currentDispatcher) return currentDispatcher
throw Error("Hooks can't be called")
}
function useXXX(...args) {
const dispatcher = resolveDispatcher()
return dispatcher.useXXX(...args)
}
function renderRoot() {
currentDispatcher = enableHooks ? dispatcherWithHooks : dispatcherWithoutHooks
performWork()
currentDispatcher = null
}

Dispatcher implementation in a nutshell.

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:

The hooks queue

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:

{
foo: 'foo',
bar: 'bar',
baz: 'baz',
}

React state — the old way.

But when dealing with hooks it should be viewed as a queue, where each node represents a single model of the state:

{
memoizedState: 'foo',
next: {
memoizedState: 'bar',
next: {
memoizedState: 'bar',
next: null
}
}
}

React state — the new way.

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:

  • 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.

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()](https://github.com/facebook/react/tree/5f06576f51ece88d846d01abd2ddd575827c6127/react-reconciler/src/ReactFiberHooks.js:123 "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.

let currentlyRenderingFiber
let workInProgressQueue
let currentHook
// Source: https://github.com/facebook/react/tree/5f06576f51ece88d846d01abd2ddd575827c6127/react-reconciler/src/ReactFiberHooks.js:123
function prepareHooks(recentFiber) {
currentlyRenderingFiber = workInProgressFiber
currentHook = recentFiber.memoizedState
}
// Source: https://github.com/facebook/react/tree/5f06576f51ece88d846d01abd2ddd575827c6127/react-reconciler/src/ReactFiberHooks.js:148
function finishHooks() {
currentlyRenderingFiber.memoizedState = workInProgressHook
currentlyRenderingFiber = null
workInProgressHook = null
currentHook = null
}
// Source: https://github.com/facebook/react/tree/5f06576f51ece88d846d01abd2ddd575827c6127/react-reconciler/src/ReactFiberHooks.js:115
function resolveCurrentlyRenderingFiber() {
if (currentlyRenderingFiber) return currentlyRenderingFiber
throw Error("Hooks can't be called")
}
// Source: https://github.com/facebook/react/tree/5f06576f51ece88d846d01abd2ddd575827c6127/react-reconciler/src/ReactFiberHooks.js:267
function createWorkInProgressHook() {
workInProgressHook = currentHook ? cloneHook(currentHook) : createNewHook()
currentHook = currentHook.next
workInProgressHook
}
function useXXX() {
const fiber = resolveCurrentlyRenderingFiber()
const hook = createWorkInProgressHook()
// ...
}
function updateFunctionComponent(recentFiber, workInProgressFiber, Component, props) {
prepareHooks(recentFiber, workInProgressFiber)
Component(props)
finishHooks()
}

*Hooks queue implementation in a nutshell.*

Once an update has finished, a function named [finishHooks()](https://github.com/facebook/react/tree/5f06576f51ece88d846d01abd2ddd575827c6127/react-reconciler/src/ReactFiberHooks.js:148 "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:

const ChildComponent = () => {
useState('foo')
useState('bar')
useState('baz')
return null
}
const ParentComponent = () => {
const childFiberRef = useRef()
useEffect(() => {
let hookNode = childFiberRef.current.memoizedState
assert(hookNode.memoizedState, 'foo')
hookNode = hooksNode.next
assert(hookNode.memoizedState, 'bar')
hookNode = hooksNode.next
assert(hookNode.memoizedState, 'baz')
})
return (
<ChildComponent ref={childFiberRef} />
)
}

An external read of a component’s memoized state.

Let’s get more specific and talk about individual hooks, starting with the most common of all — the state hook:

State hooks

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:

function basicStateReducer(state, action) {
return typeof action === 'function' ? action(state) : action;
}

State hook reducer, aka basic state reducer.

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. *T̶̶̶h̶̶̶i̶̶̶s̶̶̶ ̶̶̶i̶̶̶s̶̶̶n̶̶̶’̶̶̶t̶̶̶ ̶̶̶d̶̶̶o̶̶̶c̶̶̶u̶̶̶m̶̶̶e̶̶̶n̶̶̶t̶̶̶e̶̶̶d̶̶̶ ̶̶̶a̶̶̶n̶̶̶y̶̶̶w̶̶̶h̶̶̶e̶̶̶r̶̶̶e̶̶̶ ̶̶̶i̶̶̶n̶̶̶ ̶̶̶t̶̶̶h̶̶̶e̶̶̶ ̶̶̶o̶̶̶f̶̶̶f̶̶̶i̶̶̶c̶̶̶i̶̶̶a̶̶̶l̶̶̶ ̶̶̶R̶̶̶e̶̶̶a̶̶̶c̶̶̶t̶̶̶ ̶̶̶d̶̶̶o̶̶̶c̶̶̶u̶̶̶m̶̶̶e̶̶̶n̶̶̶t̶̶̶a̶̶̶t̶̶̶i̶̶̶o̶̶̶n̶̶̶ ̶̶̶(̶̶̶a̶̶̶s̶̶̶ ̶̶̶f̶̶̶o̶̶̶r̶̶̶ ̶̶̶t̶̶̶h̶̶̶e̶̶̶ ̶̶̶t̶̶̶i̶̶̶m̶̶̶e̶̶̶ ̶̶̶t̶̶̶h̶̶̶i̶̶̶s̶̶̶ ̶̶̶a̶̶̶r̶̶̶t̶̶̶i̶̶̶c̶̶̶l̶̶̶e̶̶̶ ̶̶̶w̶̶̶a̶̶̶s̶̶̶ ̶̶̶w̶̶̶r̶̶̶i̶̶̶t̶̶̶t̶̶̶e̶̶̶n̶̶̶)̶̶̶ ̶̶̶a̶̶̶n̶̶̶d̶̶̶ ̶̶̶ ̶t̶h̶a̶t̶’̶s̶ ̶a̶ ̶p̶i̶t̶y̶ ̶b̶e̶c̶a̶u̶s̶e̶ ̶i̶t̶’̶s̶ ̶e̶x̶t̶r̶e̶m̶e̶l̶y̶ ̶u̶s̶e̶f̶u̶l̶!̶ 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:

const ParentComponent = () => {
const [name, setName] = useState()
return (
<ChildComponent toUpperCase={setName} />
)
}
const ChildComponent = (props) => {
useEffect(() => {
props.toUpperCase((state) => state.toUpperCase())
}, [true])
return null
}

Returning a new state relatively to the old one.

Lastly, effect hooks — which made a major impact on a component’s life cycle and how it works:

Effect hooks

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:

  • 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.

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.
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:

  • 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.

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):

  • 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.

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):

const NoEffect = /* */ 0b00000000;
const UnmountSnapshot = /* */ 0b00000010;
const UnmountMutation = /* */ 0b00000100;
const MountMutation = /* */ 0b00001000;
const UnmountLayout = /* */ 0b00010000;
const MountLayout = /* */ 0b00100000;
const MountPassive = /* */ 0b01000000;
const UnmountPassive = /* */ 0b10000000;

Supported hook effect types by React.

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.

const effectTag = MountPassive | UnmountPassive
assert(effectTag, 0b11000000)
assert(effectTag & MountPassive, 0b10000000)

An example which shows how to use React’s binary design pattern.

Here are the supported hook effect types by React along with their tags (see 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.

And here’s how React checks for behavior implementation (see implementation):

if ((effect.tag & unmountTag) !== NoHookEffect) {
// Unmount
}
if ((effect.tag & mountTag) !== NoHookEffect) {
// Mount
}

A real snapshot from React’s implementation.

So, based on what we’ve just learned regards effect hooks, we can actually inject an effect to a certain fiber externally:

function injectEffect(fiber) {
const lastEffect = fiber.updateQueue.lastEffect
const destroyEffect = () => {
console.log('on destroy')
}
const createEffect = () => {
console.log('on create')
return destroy
}
const injectedEffect = {
tag: 0b11000000,
next: lastEffect.next,
create: createEffect,
destroy: destroyEffect,
inputs: [createEffect],
}
lastEffect.next = injectedEffect
}
const ParentComponent = (
<ChildComponent ref={injectEffect} />
)

Example of effects injection.

So that was it! What was your biggest takeout from this article? How are you gonna use this new knowledge in your React apps? Would love to see interesting comments!

JavaScript developers should you be using Web Workers?

JavaScript developers should you be using Web Workers?

Do you think JavaScript developers should be making more use of Web Workers to shift execution off of the main thread?

Originally published by David Gilbertson at https://medium.com

So, Web Workers. Those wonderful little critters that allow us to execute JavaScript off the main thread.

Also known as “no, you’re thinking of Service Workers”.

Photo by Caleb Jones on Unsplash

Before I get into the meat of the article, please sit for a lesson in how computers work:

Understood? Good.

For the red/green colourblind, let me explain. While a CPU is doing one thing, it can’t be doing another thing, which means you can’t sort a big array while a user scrolls the screen.

This is bad, if you have a big array and users with fingers.

Enter, Web Workers. These split open the atomic concept of a ‘CPU’ and allow us to think in terms of threads. We can use one thread to handle user-facing work like touch events and rendering the UI, and different threads to carry out all other work.

Check that out, the main thread is green the whole way through, ready to receive and respond to the gentle caress of a user.

You’re excited (I can tell), if we only have UI code on the main thread and all other code can go in a worker, things are going to be amazing (said the way Oprah would say it).

But cool your jets for just a moment, because websites are mostly about the UI — it’s why we have screens. And a lot of a user’s interactions with your site will be tapping on the screen, waiting for a response, reading, tapping, looking, reading, and so on.

So we can’t just say “here’s some JS that takes 20ms to run, chuck it on a thread”, we must think about where that execution time exists in the user’s world of tap, read, look, read, tap…

I like to boil this down to one specific question:

Is the user waiting anyway?

Imagine we have created some sort of git-repository-hosting website that shows all sorts of things about a repository. We have a cool feature called ‘issues’. A user can even click an ‘issues’ tab in our website to see a list of all issues relating to the repository. Groundbreaking!

When our users click this issues tab, the site is going to fetch the issue data, process it in some way — perhaps sort, or format dates, or work out which icon to show — then render the UI.

Inside the user’s computer, that’ll look exactly like this.

Look at that processing stage, locking up the main thread even though it has nothing to do with the UI! That’s terrible, in theory.

But think about what the human is actually doing at this point. They’re waiting for the common trio of network/process/render; just sittin’ around with less to do than the Bolivian Navy.

Because we care about our users, we show a loading indicator to let them know we’ve received their request and are working on it — putting the human in a ‘waiting’ state. Let’s add that to the diagram.

Now that we have a human in the picture, we can mix in a Web Worker and think about the impact it will have on their life:

Hmmm.

First thing to note is that we’re not doing anything in parallel. We need the data from the network before we process it, and we need to process the data before we can render the UI. The elapsed time doesn’t change.

(BTW, the time involved in moving data to a Web Worker and back is negligible: 1ms per 100 KB is a decent rule of thumb.)

So we can move work off the main thread and have a page that is responsive during that time, but to what end? If our user is sitting there looking at a spinner for 600ms, have we enriched their experience by having a responsive screen for the middle third?

No.

I’ve fudged these diagrams a little bit to make them the gorgeous specimens of graphic design that they are, but they’re not really to scale.

When responding to a user request, you’ll find that the network and DOM-manipulating part of any given task take much, much longer than the pure-JS data processing part.

I saw an article recently making the case that updating a Redux store was a good candidate for Web Workers because it’s not UI work (and non-UI work doesn’t belong on the main thread).

Chucking the data processing over to a worker thread sounds sensible, but the idea struck me as a little, umm, academic.

First, let’s split instances of ‘updating a store’ into two categories:

  1. Updating a store in response to a user interaction, then updating the UI in response to the data change
  2. Not that first one

If the first scenario, a user taps a button on the screen — perhaps to change the sort order of a list. The store updates, and this results in a re-rendering of the DOM (since that’s the point of a store).

Let me just delete one thing from the previous diagram:

In my experience, it is rare that the store-updating step goes beyond a few dozen milliseconds, and is generally followed by ten times that in DOM updating, layout, and paint. If I’ve got a site that’s taking longer than this, I’d be asking questions about why I have so much data in the browser and so much DOM, rather than on which thread I should do my processing.

So the question we’re faced with is the same one from above: the user tapped something on the screen, we’re going to work on that request for hopefully less than a second, why would we want to make the screen responsive during that time?

OK what about the second scenario, where a store update isn’t in response to a user interaction? Performing an auto-save, for example — there’s nothing more annoying than an app becoming unresponsive doing something you didn’t ask it to do.

Actually there’s heaps of things more annoying than that. Teens, for example.

Anyhoo, if you’re doing an auto-save and taking 100ms to process data client-side before sending it off to a server, then you should absolutely use a Web Worker.

In fact, any ‘background’ task that the user hasn’t asked for, or isn’t waiting for, is a good candidate for moving to a Web Worker.

The matter of value

Complexity is expensive, and implementing Web Workers ain’t cheap.

If you’re using a bundler — and you are — you’ll have a lot of reading to do, and probably npm packages to install. If you’ve got a create-react-app app, prepare to eject (and put aside two days twice a year to update 30 different packages when the next version of Babel/Redux/React/ESLint comes out).

Also, if you want to share anything fancier than plain data between a worker and the main thread you’ve got some more reading to do (comlink is your friend).

What I’m getting at is this: if the benefit is real, but minimal, then you’ve gotta ask if there’s something else you could spend a day or two on with a greater benefit to your users.

This thinking is true of everything, of course, but I’ve found that Web Workers have a particularly poor benefit-to-effort ratio.

Hey David, why you hate Web Workers so bad?

Good question.

This is a doweling jig:

I own a doweling jig. I love my doweling jig. If I need to drill a hole into the end of a piece of wood and ensure that it’s perfectly perpendicular to the surface, I use my doweling jig.

But I don’t use it to eat breakfast. For that I use a spoon.

Four years ago I was working on some fancy animations. They looked slick on a fast device, but janky on a slow one. So I wrote fireball-js, which executes a rudimentary performance benchmark on the user’s device and returns a score, allowing me to run my animations only on devices that would render them smoothly.

Where’s the best spot to run some CPU intensive code that the user didn’t request? On a different thread, of course. A Web Worker was the correct tool for the job.

Fast forward to 2019 and you’ll find me writing a routing algorithm for a mapping application. This requires parsing a big fat GeoJSON map into a collection of nodes and edges, to be used when a user asks for directions. The processing isn’t in response to a user request and the user isn’t waiting on it. And so, a Web Worker is the correct tool for the job.

It was only when doing this that it dawned on me: in the intervening quartet of years, I have seen exactly zero other instances where Web Workers would have improved the user experience.

Contrast this with a recent resurgence in Web Worker wonderment, and combine that contrast with the fact that I couldn’t think of anything else to write about, then concatenate that combined contrast with my contrarian character and you’ve got yourself a blog post telling you that maybe Web Workers are a teeny-tiny bit overhyped.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

An Introduction to Web Workers

JavaScript Web Workers: A Beginner’s Guide

Using Web Workers to Real-time Processing

How to use Web Workers in Angular app

Using Web Workers with Angular CLI