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} />);}
label
handleSelect
handleSelect
onClose
Each item is read by the code. None is a constant, a ref or a setter.
- Imports and values declared outside the component
conststrings, numbers andnulluseStatesetters,useReducerdispatchuseRef,useEffectEvent,startTransitionref.current
Everything this code reads is listed in box 2, and nothing else. Nobody typed it.
vscode-react-memoizecode --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.
function SaveBar({ id, save }: Props) {
return <Button onClick={() => save(id)} />;
}
function SaveBar({ id, save }: Props) {
const onClick = useCallback(() => save(id), [save, id]);
return <Button onClick={onClick} />;
}
function Board() {
return <Grid defaultSort={{{ field: "date", sortType: "desc" }}} />;
}
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, notuser - onSelect(user.id)
- depends on
onSelectanduser.id - props.onSelect(id)
- depends on
propsandid, because the method is called onprops - const a = 1, b = props.b, c = 3
- extracting
bleavesaandcas 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 conditionalreturnorthrow - In a nested helperor in a function that is not a component or a hook
- Already memoizedthe initializer is
useMemooruseCallback - 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