react-use-callback

Guide React useCallback usage for stable function references and preventing re-renders.

5|Updated Oct 17, 2011
One-click install
npx skills add https://github.com/klen/dotfiles --skill react-use-callback-klen
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: react-use-callback
Source: https://github.com/klen/dotfiles/tree/main/.config/opencode/skills/react-use-callback
Command: npx skills add https://github.com/klen/dotfiles --skill react-use-callback-klen

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill guides users on the correct and efficient application of the useCallback hook in React, preventing unnecessary re-renders and optimizing performance.

Core Features & Use Cases

  • Memoizes functions: Caches function definitions between renders to maintain referential stability.
  • Performance Optimization: Crucial for passing stable callbacks to memoized child components (React.memo).
  • Dependency Management: Ensures functions update only when their specified dependencies change.
  • Use Case: When passing a handleClick function to a MemoizedButton component, useCallback ensures MemoizedButton doesn't re-render unnecessarily on every parent re-render.

Quick Start

Use the react-use-callback skill to optimize a callback function for a memoized child component.

Frequently Asked Questions about react-use-callback

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use useCallback to prevent unnecessary re-renders in React?

Use useCallback to memoize functions passed to memoized child components, ensuring referential stability and preventing unnecessary re-renders when the parent component updates. It caches function definitions between renders.

When should I use useCallback for performance optimization in React?

Apply useCallback when passing callbacks to components wrapped in React.memo, managing effect dependencies, or returning values from custom hooks. It ensures functions update only when specified dependencies change.

What is the difference between useCallback and useMemo for React optimization?

useCallback memoizes function references to maintain referential stability, while useMemo caches computed values. Both prevent unnecessary re-renders but target different types of references in React components.

Why does my memoized child component still re-render with useCallback?

Memoized child components re-render if useCallback dependencies change frequently or if the child component is not properly wrapped in React.memo. Verify dependency arrays and component memoization to ensure stable function references.

Can I use useCallback to manage effect dependencies in React?

Yes, useCallback stabilizes function references used in useEffect dependency arrays. This prevents effects from re-running unnecessarily by ensuring the function identity remains consistent across renders unless its specified dependencies change.

What are common anti-patterns when using useCallback in React?

Common anti-patterns include overusing useCallback on functions never passed to memoized children, omitting dependencies, or wrapping every function. This adds overhead without performance benefits and can cause stale closures or unnecessary complexity.