Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readReact

Remove useMemo and useCallback under the compiler

Check which components React Compiler optimizes and which effects depend on memoized values before removing useMemo or useCallback.

You turned on React Compiler in an app full of useMemo and useCallback. Before removing a hook, check whether the component compiles and whether an effect depends on the value it returns.

What the compiler already covers

Compile this component with babel-plugin-react-compiler. List stands for the child component you render.

export function FilteredList({ items, query }) {
  const visible = items.filter((item) => item.name.includes(query))
  const onPick = (id) => console.log('picked', id)
  return <List items={visible} onPick={onPick} />
}

In the compiler output, the filter is cached by items and query. The compiler lifts onPick to module scope because it captures nothing, and it caches the <List> element by visible.

That is the work useMemo and useCallback did by hand. For new code, the React Compiler docs recommend leaving it to the compiler and reaching for the hooks only "where needed to achieve precise control".

Keep the ones an effect depends on

Inspect effects that depend on a memoized object. In this example, createConnection stands for your chat client’s connection factory.

import { useEffect, useMemo } from 'react'

export function ChatRoom({ roomId }) {
  const options = useMemo(() => ({ serverUrl: 'https://chat.example', roomId }), [roomId])
  useEffect(() => {
    const connection = createConnection(options)
    connection.connect()
    return () => connection.disconnect()
  }, [options])
  return <h1>Room {roomId}</h1>
}

Compiler 1.0 can cache options by roomId with or without the hook. The 1.0 release post recommends testing removals because the compiled result can change. To eliminate the object dependency here, create options inside the effect and depend on roomId directly.

Keep the ones in components it skips

A diagnostic can cause the compiler to skip an entire component. This example reads and writes a ref during render.

import { useMemo, useRef } from 'react'

export function Report({ rows }) {
  const renders = useRef(0)
  renders.current += 1
  const total = useMemo(() => rows.reduce((sum, row) => sum + row.amount, 0), [rows])
  return <p>{total} after {renders.current} renders</p>
}

The compiler reports Cannot access refs during render and leaves Report untouched, so the useMemo is the only memoization it has. Delete it and the sum reruns on every render. Fix the component first; the react-hooks/refs rule in eslint-plugin-react-hooks points at the line.

Delete the rest one component at a time

For existing code the docs give two options: leave the memoization in place, or test before removing it, because "removing it can change compilation output". Remove it in small commits, one component per commit, and check any effect that used the value before moving on.

Check the dependency arrays during the audit. If filter comes from props, useMemo(() => data.filter(filter), [data]) can return a stale result when it changes. Include filter in the dependencies or compute the result during render.