Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
useEffect は使いたくないのですが、ではどうしたらいいですか
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
jsakamoto
April 08, 2026
Programming
31
1
Share
useEffect は使いたくないのですが、ではどうしたらいいですか
React .study vol.01@sapporo (
https://hokkaido-js.connpass.com/event/388202/
) での発表資料です。
jsakamoto
April 08, 2026
More Decks by jsakamoto
See All by jsakamoto
開発したプレゼン用ツールが15年経っても誰も使ってくれない話
jsakamoto
0
59
UI コンポーネントカタログに MCP サーバー機能を追加する試み、そしてその結果
jsakamoto
1
94
いいね が燃料! 「自分のOSS」で1億ダウンロード突破の開発者が語る OSS 開発のリアル
jsakamoto
0
220
minify の効果を最大限に引き出す TypeScript コードを書く
jsakamoto
2
360
JavaScript 以外の言語によるフロントエンド Web 開発が既に実用段階である話
jsakamoto
0
2.8k
ベクトル化を使った意味検索を、簡単にアプリケーションに搭載できる時代になっていた件。
jsakamoto
2
370
CSR? SSR? C# で作る Web アプリフレームワーク Blazor のレンダリング方式を整理する
jsakamoto
0
970
UI コンポーネントカタログ “Storybook” を、C# で SPA が作れる Blazor で再実装した話
jsakamoto
0
2.1k
Evolution of Blazor in .NET 8 - Exploring the Multi-Page Apps Implementation by Blazor!
jsakamoto
1
980
Other Decks in Programming
See All in Programming
モダンOBSプラグイン開発
umireon
0
200
レガシーPHP転生 〜父がドメインエキスパートだったのでDDD+Claude Codeでチート開発します〜
panda_program
0
210
[PHPerKaigi 2026]PHPerKaigi2025の企画CodeGolfが最高すぎて社内で内製して半年運営して得た内製と運営の知見
ikezoemakoto
0
320
VueエンジニアがReactを触って感じた_設計の違い
koukimiura
0
150
Mastering Event Sourcing: Your Parents Holidayed in Yugoslavia
super_marek
0
140
AI時代のシステム設計:ドメインモデルで変更しやすさを守る設計戦略
masuda220
PRO
7
1.2k
PHP 7.4でもOpenTelemetryゼロコード計装がしたい! / PHPerKaigi 2026
arthur1
1
490
ポーリング処理廃止によるイベント駆動アーキテクチャへの移行
seitarof
3
1.3k
GoのDB アクセスにおける 「型安全」と「柔軟性」の両立 - Bob という選択肢
tak848
0
310
「接続」—パフォーマンスチューニングの最後の一手 〜点と点を結ぶ、その一瞬のために〜
kentaroutakeda
5
2.4k
Radical Imagining - LIFT 2025-2027 Policy Agenda
lift1998
0
230
脱 雰囲気実装!AgentCoreを良い感じにWEBアプリケーションに組み込むために
takuyay0ne
3
430
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Docker and Python
trallard
47
3.8k
The Cult of Friendly URLs
andyhume
79
6.8k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
190
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
310
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
27
3.4k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Making the Leap to Tech Lead
cromwellryan
135
9.8k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
110
Transcript
useEffect は使いたくないのですが、 ではどうしたらいいですか React .study vol.01@sapporo
お題 GitHub の公開 REST API を 使って、指定のリポジトリの 貢献者を取得し、一覧表示す る React
アプリケーションを 作成します。
前提・制約 • 実用レベルのアプリケーションではなく、新人教育やブログ記事向けといった、 サンプルコードとしての作成 • React 以外の外部ライブラリは使用したくない • 単一のコンポーネントで完結させたい
かつてはこう書いていました
import "./App.css" ; import { useEffect, useState } from "react"
; import type { Contributor } from "./types/Contributor" ; function App() { const [ contributors , setContributors ] = useState <Contributor []>([]); useEffect (() => { ( async () => { const response = await fetch ( " http s :// api.github. com/ repos/jsakamoto/BlazingStory/contributors" ); const data : Contributor [] = await response . json (); setContributors ( data ); })(); }, []); return ( <section > <h1>Contributors </ h1> <div className ="contributors - grid" > { contributors . map(( contributor ) => ( <a key ={ contributor .login } href ={ contributor .html_url } target ="_blank" title ={ contributor .login } rel =" no <img src ={ contributor .avatar_url } alt ={ contributor .login } width ={ 40 } height ={ 40 } loading ="lazy" /> </ a> )) } </ div > </ section > ); } export default App;
初期データの fetch に useEffect は使いたくない • REST API の fetch
に useEffect は使うなとみんなが言ってる • いちどレンダリングが完了し DOM 要素の構築・マウントが済んでから、 初期データの fetch を開始することになる • 初期データの fetch なのに何故空データでのレンダリング完了を待たねばならぬのか • DOM 要素の構築・更新が完了したことを通知する Hook を、コンポーネント の初期データ取得に "流用" している違和感
React 19 からこう書いています
import "./App.css" ; import { Suspense, use, useState } from
"react" ; import type { Contributor } from "./types/Contributor" ; function App() { const [ contributors ] = useState ( async () => { const response = await fetch ( " http s :// api.github. com/ repos/jsakamoto/BlazingStory/contributors" ); const data : Contributor [] = await response . json (); return data ; }); return ( <section > <h1>Contributors </ h1> <div className ="contributors - grid" > <Suspense fallback ={ <p>Loading... </ p>} > <ContributorsList contributors ={ contributors } /> </ Suspense > </ div > </ section > ); } function ContributorsList ( props : { contributors : Promise <Contributor []> }) { const contributors = use ( props .contributors ); return ( <> { contributors . map(( contributor ) => ( <a key ={ contributor .login } href ={ contributor .html_url } target ="_blank" title ={ contributor .login } rel =" noope
use と Suspense で自然に書けるようになった • DOM の初回レンダリング完了を待つことなく、fetch を開始できる • コードの流れや意図もいい感じ
• useState に渡す初期値関数で Promise を返すところは「おぉ?」と思うかも? • でもいったん理解してしまえば、妥当と感じられると思う
しかし限界も • 単一コンポーネントに収まらなかったのが惜しい • use フック※は Suspense 配下のコンポーネントでしか使えない • なのでコンポーネント分割は不可避
• 「これはサンプルなので...」 がどこまで許されるか? • 実用レベルのアプリケーションでは、このような素朴な実装では、レースコンディション やネットワークウォーターフォールといった課題に対処できない ※2026/04/08 当日、会場で教えていただきましたが use はフックではなくて API でした 出典: https://react.dev/reference/react/use
SWR を使ってみます
import "./App.css" ; import useSWR from " swr " ;
import type { Contributor } from "./types/Contributor" ; function App() { const { data: contributors , isLoading } = useSWR ( " http s :// api.github. com/ repos/jsakamoto/BlazingStory/contributors" const response = await fetch ( url ); const data : Contributor [] = await response . json (); return data ; }); return ( <section > <h1>Contributors </ h1> <div className ="contributors - grid" > { isLoading ? ( <p>Loading... </ p> ) : ( contributors ?. map(( contributor ) => ( <a key ={ contributor .login } href ={ contributor .html_url } target ="_blank" title ={ contributor .login } rel =" n <img src ={ contributor .avatar_url } alt ={ contributor .login } width ={ 40 } height ={ 40 } loading ="lazy" /> </ a> )) ) } </ div > </ section > ); } export default App;
単一コンポーネントに収まった • かつ、実用レベルのアプリケーション実装においても流用できる • 公式による "データ取得にはライブラリを使え" の推奨に従った形 • ただし、SWR という外部ライブラリに依存してしまった
• サンプルコードとしての中立性が失われる • なぜ TanStack Query や React Router を採用しなかったのか、みたいなツッコミも
まとめ
脱 useEffect には成功しましたが... • 実用レベルでは、外部ライブラリで非同期データを取得すべき、ですね? • React 18 以前でも OK
• ただしどのライブラリに依存するべきか悩む必要が発生 • React 19 の use フック※を使えばかなり自然に書ける • ただし単一コンポーネントには収まらない • 実用レベルでは問題ある点を「サンプルだから...」で押し通せるか? • サンプルコードという文脈では、どちらの道を選ぶか悩ましい ※2026/04/08 当日、会場で教えていただきましたが use はフックではなくて API でした 出典: https://react.dev/reference/react/use
Learn, Practice, Share.