复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-you-might-not-need-a-callback-e71cb0beb15d ,按照其中的说明把「you-might-not-need-a-callback」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
Arguments:
User arguments: $ARGUMENTS
Read before analyzing:
useCallback is only useful when something observes the reference. Ask: does anything care if this function gets a new identity on re-render?
Observers that care about reference stability:
useEffect that lists the function in its deps arrayuseMemo that lists the function in its deps arrayuseCallback that lists the function in its deps arrayReact.memo that receives the function as a propIf none of those apply — if the function is only called inline, or passed to a non-memoized child, or assigned to a native element event — the reference is unobserved and useCallback adds overhead with zero benefit.
<button onClick={fn}>). Nothing re-runs or bails out based on reference identity. Remove useCallback.<button onClick={fn}> — React never does reference equality on native element props. No benefit.useMemo on the return value instead, or restructure.useCallback. Functions a hook returns are wrapped by convention (.claude/rules/sim-hooks.md Rule 4, matching react.dev) — do not flag those for lacking an observer, but still check their dependency arrays (patterns 2-5 apply to them as much as to any other useCallback).useCallback with an observer from the list aboveuseRef + callback with empty deps that reads the ref inside — correct, do not flag:const idRef = useRef(id)
useEffect(() => { idRef.current = id }, [id])
const fetchData = useCallback(async () => {
// use idRef.current instead of id
}, []) // empty deps because refs are used