Refs, Events and Escape Hatches
Tags: react, javascript, typescript, webdev
Published: 2022-09-30 20:05:36

I hope that after reading part one about [stale closures in React](https://tkdodo.eu/blog/hooks-dependencies-and-stale-closures), you now have a good understanding of why they can occur and how to avoid them.

However, it is sometimes quite cumbersome to have to think about referential stability when passing functions around, and it can also get a bit boilerplate-y with all those additional _useCallbacks_.

I already left a bunch of clues in the previous article that there might be a way to make this easier, so here we go:

## Photoshop

Remember this sentence?

> The thing about the picture is - it cannot change. Once we have taken it, its content is sealed (unless we use photoshop).

In React, we almost always think in immutability. _Props_ cannot be changed (from within a component), and _state_ updates are merely scheduled and also need to happen in an immutable way. Everything we see and work with is essentially a _const_.

We always see the things from the time they were created, but if they are _mutated_ in place, we would still see the latest value. Let's take the basic example from part 1 and store the value in a mutable variable outside the React component:(note: examples are interactive on my blog: https://tkdodo.eu/blog/refs-events-and-escape-hatches)

```js
let count = 1
function App() {
  const rerender = React.useReducer(() => ({}))[1]

  // 😱 logCount is stable, but it still sees
  // the latest value of count
  const logCount = React.useCallback(() => {
    console.log(count)
  }, [])

  return (
    <div>
      <div>count is {count}</div>
      <button
        onClick={() => {
          count++
          rerender()
        }}
      >
        increment
      </button>
      <button onClick={logCount}>log</button>
    </div>
  )
}
```

Now it's hopefully obvious that this is _not_ how you would work with React. Having a variable outside the component means it's shared between component instances, and React doesn't know about it, which is why we have to manually force our component to re-render.

But it showcases one thing: _mutating_ a variable in place bypasses our mental modal of pictures that cannot change.

Mutations are literally photoshop. They can alter existing images after they have been taken. And what's the React way to hold mutable values? [Refs](https://beta.reactjs.org/learn/escape-hatches#referencing-values-with-refs)!

## Refs

Refs are just bags that can contain an arbitrary mutable value. The ref itself is referentially stable, so we don't have to include it in dependency arrays, and the _.current_ property has the actual value. Refs offer an escape hatch from Reacts one way data flow model, because updates on refs are not going to make your component to re-render. This comes in quite handy if we have to pass functions around, e.g. to custom hooks.

As an example, let's implement a somewhat common use-case of storing a value and triggering a callback after some debounced time:

```js
export const useDebouncedState = (callback, delay) => {
  const [value, setValue] = React.useState('')

  React.useEffect(() => {
    const timeoutId = setTimeout(() => {
      if (value) {
        callback(value)
      }
    }, delay)

    return () => {
      clearTimeout(timeoutId)
    }
  }, [value, delay, callback])

  return [value, setValue]
}
```

We've added _callback_ to the effect's dependency array as the linter wants, but that has a downside: consumers will need to memoize the function, which is not a very developer friendly interface. Of course, they'll most often just want to use an inline function, without having to care about referential stability:

```js
const [state, setState] = useDebouncedState((value) => {
  alert(value)
}, 1000)
```

This will potentially run our effect too often, e.g. if the component re-renders for another reason. However, omitting the function from the dependency array is not an option, as it would potentially introduce stale closures.

### The latest ref

I first read about a pattern called _the latest ref_ in [this article](https://epicreact.dev/the-latest-ref-pattern-in-react/) by [Kent C. Dodds](https://twitter.com/kentcdodds). The idea is to store the function (our callback) in a ref so that we can omit it from the dependency array. All we then need to do is to make sure that the ref is updated when the function changes, and we can do that with an additional effect:

```js
export const useDebouncedState = (callback, delay) => {
  const [value, setValue] = React.useState('')
  // 👇 store callback in a ref
  const ref = React.useRef(callback)

  // 👇 update the ref when the callback changes
  React.useLayoutEffect(() => {
    ref.current = callback
  }, [callback])

  React.useEffect(() => {
    const timeoutId = setTimeout(() => {
      if (value) {
        // 👇 use the ref instead of the callback
        ref.current(value)
      }
    }, delay)

    return () => {
      clearTimeout(timeoutId)
    }
  // 👇 no need to include the callback
  }, [value, delay])

  return [value, setValue]
}
```

Admittedly, this does add more boilerplate, but it pays off - especially for libraries or reusable hooks due to the improved developer experience for consumers of the hook. Also, hold on tight because React might actually ship a hook that does this for us in the future:

## useEvent

Recently, the React team has worked on an RFC about a dedicated hook for this behaviour called [useEvent](https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md). The idea is to have a hook that is similar to _useCallback_, except that it doesn't have a dependency array, but still returns a stable function reference without suffering from stale closure problems.

As pointed out in the [new beta docs](https://beta.reactjs.org/learn/separating-events-from-effects#declaring-an-event-function), those event functions behave a lot more like an event handler (hence the name). The logic inside it is not reactive, and it always sees the latest values of your props and state.

Would that hook exist already, we could rewrite our _useDebouncedState_ hook like this:

```js
export const useDebouncedState = (callback, delay) => {
  const [value, setValue] = React.useState('')

  // 👇 declare the event
  const onTimeout = React.useEvent(callback);

  React.useEffect(() => {
    const timeoutId = setTimeout(() => {
      if (value) {
        // 👇 use the event
        onTimeout(value);
      }
    }, delay);

    return () => {
      clearTimeout(timeoutId);
    };
  // 🎉 no need to include onTimeout
  }, [value, delay]);

  return [value, setValue]
}
```

This looks pretty clean to me, and if you want to have a user-land implementation of _useEvent_ right now, have a look at this implementation by [Diego Haz](https://twitter.com/diegohaz):

{% embed https://twitter.com/diegohaz/status/1522868292301606912 %}

You can use it until React ships their own hook, but be aware that you have to include the returned function in the dependency array because unlike the native implementation, the linter cannot know that the function is stable. I personally can't wait until this ships because it will simplify a lot of my code. 🙌

---

That's it for today. Feel free to reach out to me on [twitter](https://twitter.com/tkdodo)
if you have any questions, or just leave a comment below. ⬇️