react
FreeNo executable scriptsNot checkedApply when writing React components. Hook discipline, state placement, performance, async cleanup, and list keys.
About this skill
Sub-Skill: React Best Practices
Purpose: Prevents the React-specific mistakes LLMs make repeatedly — wrong state placement, stale closures, unnecessary re-renders, and broken async patterns. Concrete rules with code examples.
Rule classification
- MUST — load-bearing. Violating causes infinite loops, stale data, leaked subscriptions, or invisible UI bugs. Never break.
- SHOULD — default behavior. Deviation needs a documented reason in the code or PR.
- AVOID — usually wrong; documented exception inline where needed.
Where these rules don't strictly apply: test fixtures, Storybook stories, design-system primitives in isolation, and small in-tutorial demo components may legitimately differ. The rules below apply to production application code.
Component Design
SHOULD: Co-locate state with the component that owns it. Lift state only when two siblings genuinely share it. Lifting to a grandparent "just in case" causes unnecessary re-renders across the tree.
// Avoid: form state lifted to page-level parent function Page() { const [email, setEmail] = useState(''); return <Form email={email} setEmail={setEmail} />; } // Prefer: state lives in the component that uses it function Form() { const [email, setEmail] = useState(''); return <input value={email} onChange={e => setEmail(e.target.value)} />; }SHOULD: Split components at ~100 lines or when a section has its own data concern. One component = one responsibility. Extract
<UserAvatar>,<OrderSummary>rather than one<ProfilePage>that does everything. Exception: pages that are mostly markup with little logic may exceed 100 lines.SHOULD: Replace prop drilling beyond two levels with composition or context. Passing
userIdthrough four components to reach a button is a design smell.// Avoid: drilling through intermediaries <Layout userId={userId}><Sidebar userId={userId}><Nav userId={userId} /></Sidebar></Layout> // Prefer: context or render-prop composition <UserContext.Provider value={userId}><Layout /></UserContext.Provider>MUST: Never create component definitions inside render. Inner components are recreated on every render, destroying their state and forcing full remounts.
// Wrong function Parent() { const Child = () => <div>hello</div>; // new reference every render return <Child />; } // Correct: define outside const Child = () => <div>hello</div>; function Parent() { return <Child />; }
State Management
MUST: Never mutate state directly. React compares references. Mutating in place skips re-renders silently.
// Wrong const [items, setItems] = useState([]); items.push(newItem); // mutation — React does not re-render setItems(items); // Correct setItems(prev => [...prev, newItem]);SHOULD: Compute derived values in render, not in useEffect. If a value can be calculated from existing state/props, calculate it inline. useEffect for derived state creates a one-render lag and extra state variables.
// Avoid const [fullName, setFullName] = useState(''); useEffect(() => { setFullName(`${first} ${last}`); }, [first, last]); // Prefer const fullName = `${first} ${last}`;MUST: Use useRef for values that must not trigger re-renders (timers, DOM nodes, previous values). Use useState for anything the UI depends on. Mixing them causes invisible bugs.
// Wrong: ref for displayed value const count = useRef(0); count.current++; // UI never updates // Wrong: state for a timer ID const [timerId, setTimerId] = useState(null); // triggers re-render on set // Correct const timerId = useRef(null);
Hooks
MUST: Specify complete dependency arrays in useEffect. Omitting a dependency creates a stale closure. The ESLint rule
exhaustive-depsmust be enabled and respected.// Wrong: stale closure over userId useEffect(() => { fetchUser(userId); }, []); // runs once, userId never updates // Correct useEffect(() => { fetchUser(userId); }, [userId]);MUST: Cancel async operations in useEffect cleanup. Fetch without an AbortController causes state updates on unmounted components and race conditions.
useEffect(() => { const controller = new AbortController(); fetch(`/api/user/${id}`, { signal: controller.signal }) .then(r => r.json()) .then(setUser) .catch(err => { if (err.name !== 'AbortError') setError(err); }); return () => controller.abort(); }, [id]);MUST: Never call hooks conditionally or inside loops. Hook call order must be identical on every render. Wrap conditional logic inside the hook body, not around the
Install react in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the react skill do?
Apply when writing React components. Hook discipline, state placement, performance, async cleanup, and list keys.
How do I install the react skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the react skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
by Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
by Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
by Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
by Anthropic