跳到主要内容
知仓学习社ZHICANG

react-hooks

Deep expertise in React hooks patterns including custom hooks, composition, optimization, and testing strategies.

不碰外部(只输出文字)无严重或高危命中a5c-ai/babysitter

它会碰到什么

扫了多少2 个文本文件,6 KB
它会碰到什么不碰外部(只输出文字)
命中总数1 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

React Hooks Skill

Expert assistance for implementing React hooks with advanced patterns, composition strategies, and performance optimization.

Capabilities

  • Design and implement custom hooks with proper abstractions
  • Compose hooks for complex state and side effect management
  • Optimize hook performance with memoization patterns
  • Implement hooks for data fetching, subscriptions, and local storage
  • Test custom hooks with React Testing Library
  • Apply hooks rules and best practices

Usage

Invoke this skill when you need to:

  • Create custom hooks for reusable logic
  • Optimize existing hooks for performance
  • Implement complex state logic with useReducer
  • Build data fetching hooks with caching
  • Create form handling hooks

Inputs

| Parameter | Type | Required | Description |

|-----------|------|----------|-------------|

| hookName | string | Yes | Name of the hook (use prefix) |

| purpose | string | Yes | What the hook should accomplish |

| parameters | array | No | Input parameters for the hook |

| returnType | string | No | Expected return value structure |

| dependencies | array | No | External dependencies needed |

Configuration Example

{
  "hookName": "useDebounce",
  "purpose": "Debounce a value with configurable delay",
  "parameters": [
    { "name": "value", "type": "T" },
    { "name": "delay", "type": "number" }
  ],
  "returnType": "T"
}

Hook Patterns

State Management Hooks

// useReducer for complex state
function useToggle(initialValue = false) {
  const [value, setValue] = useState(initialValue);
  const toggle = useCallback(() => setValue(v => !v), []);
  const setTrue = useCallback(() => setValue(true), []);
  const setFalse = useCallback(() => setValue(false), []);
  return { value, toggle, setTrue, setFalse };
}

Effect Hooks

// useEffect with cleanup
function useEventListener<K extends keyof WindowEventMap>(
  eventName: K,
  handler: (event: WindowEventMap[K]) => void,
  element: Window | HTMLElement = window
) {
  const savedHandler = useRef(handler);

  useEffect(() => {
    savedHandler.current = handler;
  }, [handler]);

  useEffect(() => {
    const eventListener = (event: WindowEventMap[K]) => savedHandler.current(event);
    element.addEventListener(eventName, eventListener);
    return () => element.removeEventListener(eventName, eventListener);
  }, [eventName, element]);
}

Data Fetching Hooks

function useFetch<T>(url: string, options?: RequestInit) {
  const [data, setData] = useState<T | null>(null);
  const [error, setError] = useState<Error | null>(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const abortController = new AbortController();

    const fetchData = async () => {
      try {
        setLoading(true);
        const response = await fetch(url, {
          ...options,
          signal: abortController.signal,
        });
        if (!response.ok) throw new Error(response.statusText);
        const json = await response.json();
        setData(json);
      } catch (err) {
        if (err instanceof Error && err.name !== 'AbortError') {
          setError(err);
        }
      } finally {
        setLoading(false);
      }
    };

    fetchData();
    return () => abortController.abort();
  }, [url]);

  return { data, error, loading };
}

Best Practices

  • Always follow the Rules of Hooks
  • Use the exhaustive-deps ESLint rule
  • Memoize callbacks passed to child components
  • Return stable references when possible
  • Handle cleanup in useEffect properly
  • Use refs for values that shouldn't trigger re-renders

Target Processes

  • react-application-development
  • custom-hooks-library
  • state-management-setup
  • performance-optimization
  • frontend-testing

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。