用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/coder/xum --skill react-effects命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
| name | react-effects |
| description | Guidelines for when to use (and avoid) useEffect in React components |
Primary reference: https://react.dev/learn/you-might-not-need-an-effect
Before adding useEffect, ask:
key prop instead// ❌ Derived state stored separately
const [fullName, setFullName] = useState('');
useEffect(() => setFullName(first + ' ' + last), [first, last]);
// ✅ Calculate during render
const fullName = first + ' ' + last;
// ❌ Event logic in effect
useEffect(() => { if (isOpen) doSomething(); }, [isOpen]);
// ✅ In the handler
const handleOpen = () => { setIsOpen(true); doSomething(); };
// ❌ Reset state on prop change
useEffect(() => { setComment(''); }, [userId]);
// ✅ Use key to reset
<Profile userId={userId} key={userId} />
For subscribing to external data stores (not DOM APIs), prefer useSyncExternalStore:
// ❌ Manual subscription in effect
const [isOnline, setIsOnline] = useState(true);
useEffect(() => {
const update = () => setIsOnline(navigator.onLine);
window.addEventListener('online', update);
window.addEventListener('offline', update);
return () => { /* cleanup */ };
}, []);
// ✅ Built-in hook for external stores
const isOnline = useSyncExternalStore(
subscribe,
() => navigator.onLine, // client
() => true // server
);
Always handle race conditions with an ignore flag:
useEffect(() => {
let ignore = false;
fetchData(query).then(result => {
if (!ignore) setData(result);
});
return () => { ignore = true; };
}, [query]);
For once-per-app-load logic (not once-per-mount), use a module-level guard:
let didInit = false;
function App() {
useEffect(() => {
if (!didInit) {
didInit = true;
loadDataFromLocalStorage();
checkAuthToken();
}
}, []);
}
Or run during module initialization (before render):
if (typeof window !== 'undefined') {
checkAuthToken();
loadDataFromLocalStorage();
}
基于 SOC 职业分类