vscode-react-memoize Source on GitHub

Extract inuseMemoanduseCallback In one click with the dependency array already filled in

Put the cursor on a variable, a function, or a prop written inline in your JSX, then open the lightbulb. The array is the one exhaustive-deps would ask for.

Install for VS Code
code --install-extension apperside.vscode-react-memoize
Buy me a coffee
1. Code to declare
Profile.tsx
import { useMemo } from "react";
function Profile({ user, onSelect }) { const label = useMemo(() => user.name.toUpperCase(), [user.name]); const handleSelect = useCallback(() => onSelect(user.id), [onSelect, user.id]); const onClose = useCallback(() => onSelect(null), [onSelect]); return ( <Tag title={label} onClick={handleSelect} onClose={() => onSelect(null)onClose} /> ); }
2. Items carried
user.name for label
onSelect for handleSelect
user.id for handleSelect
onSelect for onClose

Each item is read by the code. None is a constant, a ref or a setter.

3. Exempt, not declared
  • Imports and values declared outside the component
  • const strings, numbers and null
  • useState setters, useReducer dispatch
  • useRef, useEffectEvent, startTransition
  • ref.current
4. Declaration

Everything this code reads is listed in box 2, and nothing else. Nobody typed it.

Declarantvscode-react-memoize
Install for VS Code
code --install-extension apperside.vscode-react-memoize

What it wraps

A function becomes useCallback. Anything else becomes useMemo. You only see the action that applies, and the hook is added to your existing react import.

If the file imports React as a namespace, the call is written React.useMemo and the import is left alone.

You select You get
const label = user.name const label = useMemo(() => user.name, [user.name])
const { name } = user const { name } = useMemo(() => user, [user])
const handle = () => onSelect(id) const handle = useCallback(() => onSelect(id), [onSelect, id])
function handle() { onSelect(id); } const handle = useCallback(() => { onSelect(id); }, [onSelect, id])
onSelect={(row) => save(row.id)} in JSX A named useCallback above the return, with parameter types read from the TypeScript language service

Inline props too

An arrow function, an object or an array written straight into a JSX prop is a new value on every render. Put the cursor on the value and the same lightbulb lifts it above the return as a named useCallback or useMemo. The name comes from the prop and is ready to rename.

It is not offered inside a useMemo or useCallback callback, where the value is already recreated only when that hook reruns, and not when the value reads something that exists only inside a nested callback, like a map parameter.

Before
function SaveBar({ id, save }: Props) {
  return <Button onClick={() => save(id)} />;
}
After
function SaveBar({ id, save }: Props) {
  const onClick = useCallback(() => save(id), [save, id]);
  return <Button onClick={onClick} />;
}
Before
function Board() {
  return <Grid defaultSort={{{ field: "date", sortType: "desc" }}} />;
}
After
function Board() {
  const defaultSort = useMemo(() => ({ field: "date", sortType: "desc" }), []);
  return <Grid defaultSort={defaultSort} />;
}

Only what the code reads

A prop is recorded as the value the expression actually reads, which is the array exhaustive-deps would ask for if yours were empty.

user.name.toUpperCase()
depends on user.name, not user
onSelect(user.id)
depends on onSelect and user.id
props.onSelect(id)
depends on props and id, because the method is called on props
const a = 1, b = props.b, c = 3
extracting b leaves a and c as they were

When the lightbulb stays quiet

The new hook call has to be unconditional. The action is offered only for a declaration that sits directly in the body of a function component or a custom hook.

  • Inside an ifafter a conditional return or throw
  • In a nested helperor in a function that is not a component or a hook
  • Already memoizedthe initializer is useMemo or useCallback
  • Calls a hookthe extracted code would call one
  • var, generators, overloadsnot extracted
  • Classesthe cursor is in a class

Stop typing the array.

Install for VS Code
Languages
JavaScript, JSX, TypeScript and TSX
Requires
Visual Studio Code 1.86 or newer
Open the lightbulb
Cmd+. on macOS, Ctrl+. on Windows and Linux, or run Refactor from the Command Palette
Buy me a coffee
ko-fi.com/apperside
License
MIT, source on GitHub