Skip to content
Back to skills

React Use Effect

ASecurity

Rules for useEffect usage in React including when to avoid it and when to extract it into custom hooks. Use when writing or reviewing React components that contain or might need useEffect.

  • 3 stars
  • 0 votes
  • 0 copies
  • 7 views
  • Added May 27, 2026
data-aireactapi

Works with

  • api

Security analysis

A100/100

Scanned November 28, 2026

npx -y skills add ncaq/konoka --skill react-use-effect --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of React Use Effect?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for React Use Effect
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ncaq-react-use-effect/badge)](https://www.skillsdirectory.com/skills/ncaq-react-use-effect)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: react-use-effect
description: Rules for useEffect usage in React including when to avoid it and when to extract it into custom hooks. Use when writing or reviewing React components that contain or might need useEffect.
user-invocable: false
---

# useEffectの使いどころを見極める

`useEffect`は、ブラウザAPI、ネットワーク、サードパーティのライブラリといった外部システムとの同期だけに使います。
それ以外の用途では、先に以下の代替手段を検討してください。

- レンダリング用のデータの変換は、stateに入れずにレンダリング中に直接計算します。
- 高コストな計算のキャッシュには`useMemo`を使います。
- ユーザー操作に伴う処理は、イベントハンドラの中で行います。
- propsが変わったときのstateのリセットには、`key`を使ってコンポーネントを再マウントします。
- データの取得には、TanStack Queryなどのデータフェッチライブラリを使います。
  `useEffect`での手動のフェッチは定型のコードが冗長で、キャッシュも重複の排除もありません。

```tsx
const result = useSuspenseQuery({ queryKey: ["data", id], queryFn: () => fetchData(id) });
```

参考: [You Might Not Need an Effect](https://react.dev/learn/you-might-not-need-an-effect)

## useEffectが適切な場合

- `IntersectionObserver`や`ResizeObserver`などのブラウザAPIとの同期
- サードパーティのライブラリの初期化と破棄
- WebSocketなどの外部接続の管理

これらの場合も、より抽象度の高いフックやライブラリが使えないかを先に検討します。

## カスタムフックに切り出す

`useEffect`は、コンポーネントの本体には直接書かず、カスタムフックに切り出します。
コンポーネントはそのフックを呼ぶだけにします。

カスタムフックには、`useDocumentTitle`、`useWindowResize`、`useDeviceListSubscription`のように、
責務を表す名前を付けます。
フックの置き場所はプロジェクトの規約に従います。
典型的には`src/hooks/`のような共有のディレクトリか、
そのコンポーネントに固有のフックであれば同じディレクトリの別のファイルです。

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in data-ai →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.