by softaworks
Claude、Codex 和 Claude Code 的 AI 技能
1. 打开 Claude 聊天界面
2. 点击下方 "📋 复制" 按钮
3. 粘贴到 Claude 聊天框中并发送
4. 输入 "使用 react-useeffect 技能" 开始使用
=== react-useeffect 技能 === 作者: softaworks 描述: Claude、Codex 和 Claude Code 的 AI 技能 使用方法: 1. 调用技能: "使用 react-useeffect 技能" 2. 提供相关信息: 根据技能要求提供必要参数 3. 查看结果: 技能会返回处理结果 示例: "使用 react-useeffect 技能,帮我分析一下这段代码"
这种方法适用于所有 Claude 用户,不需要安装额外工具。
productivity
medium
A comprehensive guide teaching when to use useEffect in React, and more importantly, when NOT to use it. This skill is based on official React documentation and provides practical alternatives to common useEffect anti-patterns.
Effects are an escape hatch from React's reactive paradigm. They let you synchronize with external systems like browser APIs, third-party widgets, or network requests. However, many developers overuse Effects for tasks that React handles better through other means.
This skill helps you:
useMemo, key prop, and event handlersUse this skill when you're:
useEffect codeuseState to store derived valuesTrigger phrases:
This skill provides guidance through three key resources:
The skill teaches you to ask the right questions:
Visual table showing the DO/DON'T for common scenarios:
Clear flowchart that guides you from "Need to respond to something?" to the correct solution:
Detailed examples of 9 common mistakes:
Each anti-pattern includes:
8 proven patterns to replace unnecessary Effects:
useMemo for expensive calculationskey prop to reset stateuseSyncExternalStore for external storesBad - Unnecessary Effect:
function Form() {
const [firstName, setFirstName] = useState('Taylor');
const [lastName, setLastName] = useState('Swift');
const [fullName, setFullName] = useState('');
useEffect(() => {
setFullName(firstName + ' ' + lastName);
}, [firstName, lastName]);
}
Good - Calculate during render:
function Form() {
const [firstName, setFirstName] = useState('Taylor');
const [lastName, setLastName] = useState('Swift');
const fullName = firstName + ' ' + lastName; // Just compute it
}
Bad - Effect to reset:
function ProfilePage({ userId }) {
const [comment, setComment] = useState('');
useEffect(() => {
setComment('');
}, [userId]);
}
Good - Key prop:
function ProfilePage({ userId }) {
return <Profile userId={userId} key={userId} />;
}
function Profile({ userId }) {
const [comment, setComment] = useState(''); // Resets automatically
}
Bad - Race condition:
function SearchResults({ query }) {
const [results, setResults] = useState([]);
useEffect(() => {
fetchResults(query).then(json => {
setResults(json); // "hello" response may arrive after "hell"
});
}, [query]);
}
Good - Cleanup flag:
function SearchResults({ query }) {
const [results, setResults] = useState([]);
useEffect(() => {
let ignore = false;
fetchResults(query).then(json => {
if (!ignore) setResults(json);
});
return () => { ignore = true; };
}, [query]);
}
Bad - Effect watching state:
function ProductPage({ product, addToCart }) {
useEffect(() => {
if (product.isInCart) {
showNotification(`Added ${product.name}!`);
}
}, [product]);
function handleBuyClick() {
addToCart(product);
}
}
Good - Handle in event:
function ProductPage({ product, addToCart }) {
function handleBuyClick() {
addToCart(product);
showNotification(`Added ${product.name}!`);
}
}
Effects are appropriate for:
useSyncExternalStore)Avoid Effects for:
const fullName = firstName + ' ' + lastNamekey prop to create a fresh component instanceBefore adding an Effect, ask: "Is there an external system involved?" If no, you probably don't need an Effect.
If you can calculate a value from props or state, don't store it in state with an Effect updating it.
useMemokey propuseSyncExternalStoreIf your Effect subscribes, fetches, or sets timers, return a cleanup function to prevent memory leaks and race conditions.
Multiple Effects triggering each other causes unnecessary re-renders and makes code hard to follow. Calculate everything in one place (usually an event handler).
React 18+ Strict Mode mounts components twice in development to expose missing cleanup. If your Effect breaks, you need cleanup.
For data fetching, prefer your framework's built-in solution (Next.js, Remix) or libraries (React Query, SWR) over manual Effects.
This skill includes three detailed reference documents:
Symptom: Component re-renders many times in quick succession.
Cause: Effect that sets state based on state it depends on, creating a loop.
Fix: Calculate the final value in an event handler or during render.
Symptom: UI shows outdated values briefly before updating.
Cause: Using Effect to update derived state causes an extra render pass.
Fix: Calculate derived values during render instead of in state.
Symptom: Fast typing shows results for old queries after new ones.
Cause: Missing cleanup in data fetching Effect.
Fix: Use cleanup flag (ignore variable) or AbortController.
Symptom: Effect runs twice on component mount in development.
Cause: React 18 Strict Mode intentionally mounts components twice to expose bugs.
Fix: Add proper cleanup. If it's app initialization that shouldn't run twice, use a module-level guard.
This skill is based on:
The golden rule: Effects are an escape hatch from React. If you're not synchronizing with an external system, you probably don't need an Effect.
Before writing useEffect, ask yourself:
Follow these patterns, and your React code will be more maintainable, performant, and bug-free.
View Count
0
Download Count
0
Favorite Count
0
Quality Score
52