React Hooks patterns including custom hooks and dependency management. Use when implementing component logic.
/plugin marketplace add IvanTorresEdge/molcajete.ai/plugin install ivantorresedge-react-tech-stacks-js-react@IvanTorresEdge/molcajete.aiThis skill inherits all available tools. When active, it can use any tool Claude has access to.
This skill covers React Hooks patterns, custom hooks, and dependency management.
Use this skill when:
EXTRACT AND REUSE - Extract reusable logic into custom hooks. Keep components focused on rendering.
import { useState, useCallback } from 'react';
interface UseToggleReturn {
value: boolean;
toggle: () => void;
setTrue: () => void;
setFalse: () => void;
}
export function useToggle(initialValue = false): UseToggleReturn {
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 };
}
import { useState, useEffect } from 'react';
interface UseQueryResult<T> {
data: T | null;
error: Error | null;
isLoading: boolean;
refetch: () => void;
}
export function useQuery<T>(
queryFn: () => Promise<T>,
deps: unknown[] = []
): UseQueryResult<T> {
const [data, setData] = useState<T | null>(null);
const [error, setError] = useState<Error | null>(null);
const [isLoading, setIsLoading] = useState(true);
const fetchData = useCallback(async () => {
setIsLoading(true);
setError(null);
try {
const result = await queryFn();
setData(result);
} catch (err) {
setError(err instanceof Error ? err : new Error('Unknown error'));
} finally {
setIsLoading(false);
}
}, deps);
useEffect(() => {
fetchData();
}, [fetchData]);
return { data, error, isLoading, refetch: fetchData };
}
import { useState, useCallback, ChangeEvent, FormEvent } from 'react';
interface UseFormReturn<T> {
values: T;
errors: Partial<Record<keyof T, string>>;
handleChange: (e: ChangeEvent<HTMLInputElement>) => void;
handleSubmit: (onSubmit: (values: T) => void) => (e: FormEvent) => void;
reset: () => void;
setFieldValue: (field: keyof T, value: T[keyof T]) => void;
}
export function useForm<T extends Record<string, unknown>>(
initialValues: T
): UseFormReturn<T> {
const [values, setValues] = useState<T>(initialValues);
const [errors, setErrors] = useState<Partial<Record<keyof T, string>>>({});
const handleChange = useCallback((e: ChangeEvent<HTMLInputElement>) => {
const { name, value, type, checked } = e.target;
setValues((prev) => ({
...prev,
[name]: type === 'checkbox' ? checked : value,
}));
}, []);
const handleSubmit = useCallback(
(onSubmit: (values: T) => void) => (e: FormEvent) => {
e.preventDefault();
onSubmit(values);
},
[values]
);
const reset = useCallback(() => {
setValues(initialValues);
setErrors({});
}, [initialValues]);
const setFieldValue = useCallback((field: keyof T, value: T[keyof T]) => {
setValues((prev) => ({ ...prev, [field]: value }));
}, []);
return { values, errors, handleChange, handleSubmit, reset, setFieldValue };
}
// ✅ All dependencies included
useEffect(() => {
fetchUser(userId);
}, [userId]);
// ✅ Stable callback with useCallback
const handleClick = useCallback(() => {
onClick(id);
}, [onClick, id]);
useEffect(() => {
document.addEventListener('click', handleClick);
return () => document.removeEventListener('click', handleClick);
}, [handleClick]);
// ❌ Missing dependency
useEffect(() => {
fetchUser(userId); // userId not in deps
}, []);
// ❌ Object/array causing infinite loops
useEffect(() => {
doSomething(options); // options is new object each render
}, [options]);
// ✅ Fix: Use useMemo or extract values
const { page, limit } = options;
useEffect(() => {
doSomething({ page, limit });
}, [page, limit]);
// ❌ Function recreated each render
function Component({ onSave }: { onSave: (data: Data) => void }): React.ReactElement {
useEffect(() => {
const handler = () => onSave(data);
window.addEventListener('beforeunload', handler);
return () => window.removeEventListener('beforeunload', handler);
}, [onSave, data]); // onSave might change
}
// ✅ Use useCallback in parent or useRef
function Component({ onSave }: { onSave: (data: Data) => void }): React.ReactElement {
const onSaveRef = useRef(onSave);
onSaveRef.current = onSave;
useEffect(() => {
const handler = () => onSaveRef.current(data);
window.addEventListener('beforeunload', handler);
return () => window.removeEventListener('beforeunload', handler);
}, [data]); // Stable reference
}
useEffect(() => {
const subscription = eventEmitter.subscribe(handleEvent);
return () => {
subscription.unsubscribe();
};
}, [handleEvent]);
useEffect(() => {
const controller = new AbortController();
async function fetchData(): Promise<void> {
try {
const response = await fetch(url, { signal: controller.signal });
const data = await response.json();
setData(data);
} catch (err) {
if (err instanceof Error && err.name !== 'AbortError') {
setError(err);
}
}
}
fetchData();
return () => controller.abort();
}, [url]);
useEffect(() => {
const timerId = setInterval(() => {
setCount((c) => c + 1);
}, 1000);
return () => clearInterval(timerId);
}, []);
const sortedItems = useMemo(() => {
return items.slice().sort((a, b) => a.name.localeCompare(b.name));
}, [items]);
const filteredData = useMemo(() => {
return data.filter((item) => item.status === filter);
}, [data, filter]);
// ✅ Stable function for child components
const handleSelect = useCallback((id: string) => {
setSelectedId(id);
onSelect?.(id);
}, [onSelect]);
// ✅ Stable function for effects
const fetchData = useCallback(async () => {
const result = await api.getData(params);
setData(result);
}, [params]);
// ❌ Premature optimization
const name = useMemo(() => `${firstName} ${lastName}`, [firstName, lastName]);
// ✅ Simple computation - no memoization needed
const name = `${firstName} ${lastName}`;
// ❌ Memoizing primitives
const isActive = useMemo(() => status === 'active', [status]);
// ✅ Direct comparison
const isActive = status === 'active';
// ❌ Conditional hook call
function Component({ shouldFetch }: { shouldFetch: boolean }): React.ReactElement {
if (shouldFetch) {
const data = useQuery(fetchData); // Error!
}
}
// ✅ Always call, conditionally use
function Component({ shouldFetch }: { shouldFetch: boolean }): React.ReactElement {
const data = useQuery(fetchData, { enabled: shouldFetch });
}
import { renderHook, act } from '@testing-library/react';
import { useToggle } from '../useToggle';
describe('useToggle', () => {
it('initializes with false by default', () => {
const { result } = renderHook(() => useToggle());
expect(result.current.value).toBe(false);
});
it('toggles value', () => {
const { result } = renderHook(() => useToggle());
act(() => {
result.current.toggle();
});
expect(result.current.value).toBe(true);
});
});
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.