No slash command · from web-tasuke
Avoid unnecessary useRef in React. Prefer HTML standard features, declarative libraries, state, or custom hooks. Use when writing or reviewing React components.
Ships in web-tasuke. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
useRefは命令的なコードになりやすく、
Reactの宣言的なモデルと相性がよくありません。
使う前に代替手段がないか検討してください。
ref.currentはミュータブルであり、レンダリングサイクルと同期しません。値の変更がUIに反映されず、予期しない挙動を起こしやすいですref.currentへの代入は「いつ読んでも最新」という暗黙の前提に依存しており、データフローが追いにくくなりますブラウザが提供する宣言的なHTML要素や属性で解決できないか、 まず検討してください。
// Bad: useRefで開閉を命令的に管理
const detailsRef = useRef<HTMLDetailsElement>(null);
const toggle = () => {
if (detailsRef.current) {
detailsRef.current.open = !detailsRef.current.open;
}
};
// Good: HTML標準の<details>要素なら宣言的に開閉できる
<details>
<summary>詳細を表示</summary>
<p>ここに詳細が表示されます。</p>
</details>;
// Bad: useRefでポップオーバーの表示を命令的に管理
const popoverRef = useRef<HTMLDivElement>(null);
const showPopover = () => popoverRef.current?.showPopover();
// Good: popover属性とpopoverTargetで宣言的に表現
<button popoverTarget="my-popover">開く</button>
<div id="my-popover" popover="auto">ポップオーバーの内容</div>
<details>、<dialog>、popover属性、<input type="date">など、
以前はJavaScriptが必要だった機能の多くが現在はHTML標準で提供されています。
DOM操作を抽象化してくれるライブラリがあれば、 そちらを優先してください。
// Bad: useRefとuseEffectでIntersectionObserverを管理
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
setIsVisible(entry.isIntersecting);
});
if (ref.current) observer.observe(ref.current);
return () => observer.disconnect();
}, []);
// Good: ライブラリに任せる(react-intersection-observerの例)
const { ref, inView } = useInView();
レンダリングに反映すべき値をuseRefで持っているなら、
それはuseStateで管理すべきです。
// Bad: useRefで値を保持してレンダリングと同期しない
const countRef = useRef(0);
countRef.current += 1;
// Good: stateで管理してUIに反映する
const [count, setCount] = useState(0);
useEffect内だけで使う値は、
useEffectのクロージャ内で管理すれば十分です。
// Bad: タイマーIDをuseRefで保持
const timerIdRef = useRef<number>();
useEffect(() => {
timerIdRef.current = window.setInterval(() => {
/* ... */
}, 1000);
return () => clearInterval(timerIdRef.current);
}, []);
// Good: useEffect内のローカル変数で完結
useEffect(() => {
const timerId = window.setInterval(() => {
/* ... */
}, 1000);
return () => clearInterval(timerId);
}, []);
どうしてもuseRefが必要な場合は、
カスタムフックに抽出してDOM操作の詳細を隠蔽してください。
利用側のコンポーネントは宣言的なインターフェースで使えるようになります。
以下は説明のための簡易的な例です。 実際には同等の機能を提供するライブラリがないか先に探してください。
// カスタムフックに抽出
function useResizeObserver<T extends HTMLElement>(callback: (entry: ResizeObserverEntry) => void) {
const ref = useRef<T>(null);
useEffect(() => {
const element = ref.current;
if (element == null) return;
const observer = new ResizeObserver(([entry]) => callback(entry));
observer.observe(element);
return () => observer.disconnect();
}, [callback]);
return ref;
}
// 利用側は宣言的
function ResizablePanel() {
const handleResize = useCallback((entry: ResizeObserverEntry) => {
console.log(entry.contentRect.width);
}, []);
const ref = useResizeObserver<HTMLDivElement>(handleResize);
return <div ref={ref}>...</div>;
}
上記のいずれでも解決できない場合にuseRefを直接使ってください。
Canvas操作、
サードパーティライブラリのインスタンス管理、
スクロール位置の制御など、
本質的にDOMの直接操作が必要な場面はあります。
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
17 skills in this plugin
/any-typeAvoid using any type in TypeScript. Use unknown, generics, or proper type definitions instead. Use when writing or reviewing TypeScript type annotations./aria-usageWAI-ARIA usage guidelines. Avoid redundant ARIA attributes. No ARIA is better than Bad ARIA. Use when writing or reviewing HTML/JSX with accessibility attributes./as-const-satisfiesUse `as const` and `as const satisfies Type` for constant definitions in TypeScript. Use when defining constants, config objects, or string union types./async-state-typeAvoid contradictory state types for async data fetching. Use Suspense or discriminated unions instead. Use when writing or reviewing async data fetching code in React/TypeScript./avoid-forAvoid for loops (C-style, for...of, for...in) in TypeScript/JavaScript. Prefer higher-order Array methods like map, filter, find, some, every, reduce. Use when writing or reviewing loops or iteration over arrays, objects, Map, Set, or String./dynamic-importPrefer static import over dynamic import() in TypeScript/JavaScript. Use when writing or reviewing import statements.npx claudepluginhub ncaq/konoka --plugin web-tasuke