一键导入
react-development
React patterns, anti-patterns, and performance optimization. Use when writing React components, reviewing React code, or debugging React issues.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
React patterns, anti-patterns, and performance optimization. Use when writing React components, reviewing React code, or debugging React issues.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | react-development |
| description | React patterns, anti-patterns, and performance optimization. Use when writing React components, reviewing React code, or debugging React issues. |
Modern React patterns from the official docs and community expertise.
When working with React code, apply these principles in order of importance:
Follow the 5-step process for building React UIs:
Components must be pure functions during rendering:
// ❌ Mutates external variable
let guest = 0;
function Cup() {
guest = guest + 1;
return <h2>Guest #{guest}</h2>;
}
// ✅ Uses props
function Cup({ guest }) {
return <h2>Guest #{guest}</h2>;
}
Rules:
Minimize state:
// ❌ Redundant state
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [fullName, setFullName] = useState(''); // redundant!
// ✅ Compute during render
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const fullName = firstName + ' ' + lastName;
Lift state up when siblings need the same data — move to closest common parent.
Use key to reset state:
// Forces fresh component instance when recipient changes
<Chat key={recipientId} contact={recipient} />
Only call hooks at the top level:
Only call hooks from React functions:
Effects are for synchronizing with external systems, not for state logic.
You DON'T need useEffect for:
| Scenario | Instead of Effect | Do This |
|---|---|---|
| Transform data for render | useEffect + setState | Calculate during render |
| Cache expensive calculations | useEffect + setState | useMemo |
| Reset state on prop change | useEffect + setState | Use key prop |
| Handle user events | useEffect watching state | Event handler directly |
| Notify parent of changes | useEffect calling callback | Call in event handler |
When you DO need useEffect:
Always add cleanup:
useEffect(() => {
const connection = createConnection();
connection.connect();
return () => connection.disconnect(); // cleanup
}, []);
Before reaching for memo:
childrenVirtualize large lists (50+ items):
import { FixedSizeList } from 'react-window';
<FixedSizeList height={400} itemCount={1000} itemSize={35}>
{({ index, style }) => <div style={style}>{items[index]}</div>}
</FixedSizeList>
Code split routes:
const Dashboard = lazy(() => import('./Dashboard'));
<Suspense fallback={<Loading />}>
<Dashboard />
</Suspense>
From "Writing Resilient Components":
Good for: theming, current user, routing, widely-needed state.
Try these first:
children — avoids prop drilling// ❌ Prop drilling
<Layout posts={posts} />
// ✅ Composition
<Layout>
<Posts posts={posts} />
</Layout>
| Anti-Pattern | Problem | Fix |
|---|---|---|
| Copying props to state | Stale data | Read props directly |
| Effect chains | Cascading renders | Compute in event handlers |
| Suppressing effect with refs | Hides bugs | Add proper cleanup |
| Derived state in useState | Sync issues | Compute during render |
| Missing keys in lists | Broken updates | Use stable unique IDs |
| Index as key (for reorderable lists) | State mismatch | Use item IDs |
function ProductList({ products, onSelect }) {
// Derived state — computed, not stored
const sortedProducts = useMemo(
() => [...products].sort((a, b) => a.name.localeCompare(b.name)),
[products]
);
// Event handler — not useEffect
function handleClick(product) {
onSelect(product);
}
return (
<ul>
{sortedProducts.map(product => (
<li key={product.id} onClick={() => handleClick(product)}>
{product.name}
</li>
))}
</ul>
);
}
function UserProfile({ userId }) {
const [user, setUser] = useState(null);
useEffect(() => {
let cancelled = false;
async function fetchUser() {
const data = await getUser(userId);
if (!cancelled) setUser(data);
}
fetchUser();
return () => { cancelled = true; };
}, [userId]);
if (!user) return <Loading />;
return <Profile user={user} />;
}
For detailed performance patterns, see PERFORMANCE.md.
SolidJS patterns, reactivity model, and best practices. Use when writing Solid components, reviewing Solid code, or debugging Solid issues.
Perform thorough code reviews on pull requests, diffs, or code changes. Use when asked to review code, check a PR, or provide feedback on changes.