xiaohongshu
xiaohongshu
hooks-best-practices
React Hooks patterns including custom hooks and dependency management. Use when implementing component logic.
Full skill instructions
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);
});
});
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience