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
ReactのSuspenseを使った非同期処理のエラーハンドリング
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
taro
September 08, 2022
Programming
9
7.3k
ReactのSuspenseを使った非同期処理のエラーハンドリング
「フロントエンドLT会 - vol.8」で発表したスライドです。
https://rakus.connpass.com/event/255095/
taro
September 08, 2022
Tweet
Share
More Decks by taro
See All by taro
VitestのIn-Source Testingが便利
taro28
11
3.3k
ローコードサービスの進化のためのモノレポ移行
taro28
2
530
ローコードSaaSのUXを向上させるためのTypeScript
taro28
2
1.2k
GraphQLをServer Componentsで使いたい
taro28
8
3.2k
Sequenceを理解する
taro28
1
300
propsのバケツリレー対策でGlobal_Stateを使うその前に
taro28
12
4.9k
状態ってなに?🙃
taro28
2
590
一口目から美味しいReactのスルメ本🦑
taro28
3
1.6k
T-falってすごい【社内LT】
taro28
1
370
Other Decks in Programming
See All in Programming
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
210
AIによる開発の民主化を支える コンテキスト管理のこれまでとこれから
mulyu
3
500
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
150
OCaml 5でモダンな並列プログラミングを Enjoyしよう!
haochenx
0
150
Raku Raku Notion 20260128
hareyakayuruyaka
0
370
CSC307 Lecture 05
javiergs
PRO
0
500
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
180
React 19でつくる「気持ちいいUI」- 楽観的UIのすすめ
himorishige
11
7.5k
Oxlintはいいぞ
yug1224
5
1.4k
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
230
並行開発のためのコードレビュー
miyukiw
0
1.2k
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
260
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
200
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
How to Think Like a Performance Engineer
csswizardry
28
2.5k
Practical Orchestrator
shlominoach
191
11k
Ruling the World: When Life Gets Gamed
codingconduct
0
150
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
68
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
200
Joys of Absence: A Defence of Solitary Play
codingconduct
1
290
What's in a price? How to price your products and services
michaelherold
247
13k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
170
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.3k
Transcript
ReactのSuspenseを使った 非同期処理の エラーハンドリング フロントエンドLT会 - vol.8 2022.09.08(木)
自己紹介 • taro( @taroro_tarotaro) • Shelfyで建設SaaSを作ってるエンジニア(あと少しで2年) • React, Spring, Django,
AWS, k8s
はじめに
Suspenseとは
Suspenseとは ComponentをLoading状態にできる機能
Suspenseとは ComponentをLoading状態にできる機能 これまで
Suspenseとは ComponentをLoading状態にできる機能 これまで Suspenseを使うと
Suspenseとは ComponentをLoading状態にできる機能 これまで Suspenseを使うと 取得されている前提で 宣言的に書ける!
Suspenseとは 宣言的UIフレームワーク・ライブラリのSuspenseの提供状況 • React→提供済 • Vue→実験的機能として提供 • Solid→提供済 • Svelte→未提供
Suspenseとは 宣言的UIフレームワーク・ライブラリのSuspenseの提供状況 • React→提供済 • Vue→実験的機能として提供 • Solid→提供済 • Svelte→未提供
→Suspenseは宣言的UIの標準になっていくと考えられる機能 →React以外の使い手の方にも、参考になれば嬉しいです🙂
今までの非同期処理の エラーハンドリング
Promiseチェーンでのエラーハンドリング Fetch API axios
データ取得ライブラリ(swr)でエラーハンドリング In Hooks or Component
データ取得ライブラリ(swr)でエラーハンドリング In Hooks or Component swr内ではtry-catchしている https://github.com/vercel/swr/blob/main/core/use-swr.ts#L307
Suspenseを使ってみる
Suspenseを使ってみる Suspense導入前
Suspenseを使ってみる Suspense導入前 Suspense導入後
Suspenseを使ってみる エラーが発生(ホワイトアウト) Suspense導入後
Suspenseを使ってみる エラーが発生(ホワイトアウト) →swrがエラーをcatchしていない Suspense導入後
Suspenseを使ってみる エラーが発生(ホワイトアウト) →swrがエラーをcatchしていない →なぜか? Suspense導入後
Reactの Suspenseの仕組み
ReactのSuspenseの仕組み Suspense内のComponentをrender
ReactのSuspenseの仕組み render中にpromiseがthrowされたら、直近のSuspenseがcatch
ReactのSuspenseの仕組み promiseがpendingの時は、Suspense内にfallbackをrender
ReactのSuspenseの仕組み settled(fulfilled or rejected)されたら再度render
ReactのSuspenseの仕組み renderに成功したら描画される!(再度 promiseがthrowされたらループ)
ReactのSuspenseの仕組み swrも内部でpromiseをthrow https://github.com/vercel/swr/blob/main/core/use-swr.ts#L597
ReactのSuspenseの仕組み swrも内部でpromiseをthrow promiseを扱うのはsuspenseなので、 swrはエラーをcatchしていない
じゃあどうするか?
じゃあどうするか? SWRの公式ドキュメント https://swr.vercel.app/docs/suspense
Error Boundary エラーハンドリング用ライフサイクルメソッドを使っ たComponent (V16~) • getDerivedStateFromError • ComponentDidCatch 子Componentツリーで発生した
JavaScriptのエラーをcatch • fallbackを表示 • エラーを記録 https://reactjs.org/docs/error-boundaries.html
Error Boundary https://reactjs.org/docs/error-boundaries.html Error Boundaryって、非同期処理に使 えなかった気がする。。。
Error Boundaryって、非同期処理に使 えなかった気がする。。。 Error Boundary https://reactjs.org/docs/error-boundaries.html
Error Boundaryって、非同期処理に使 えなかった気がする。。。 Error Boundary →React内部で発生するエラーしか catchできない https://reactjs.org/docs/error-boundaries.html
Error Boundary https://reactjs.org/docs/error-boundaries.html Error Boundaryって、非同期処理に使 えなかった気がする。。。 →React内部で発生するエラーしか catchできない →Suspenseだと非同期処理もReact内 部で制御されるからcatchできる
より宣言的に書けるようになった! • ローディング→Suspense • エラー→ErrorBoundary Error Boundary使う
エラーハンドリングを 分岐したい
catchしたエラーの中身を見て、 分岐してあげればOK! エラーハンドリングを分岐したい
Error Boundaryを分けたい時は、 errorをre-throwすればOK! エラーハンドリングを分岐したい
Error Boundaryを分けたい時は、 errorをre-throwすればOK! re-throwしないError Boundaryが、 エントリーポイントにあると安心 🙂 ErrorBoundary自体、low-levelのAPIで構築されて いるので柔軟性は高い エラーハンドリングを分岐したい
さいごに
さいごに Error BoundaryとSuspenseめっちゃ似てる
Error BoundaryとSuspenseめっちゃ似てる Suspense=PromiseのみをcatchするBoundary さいごに https://github.com/facebook/react/blob/main/packages/react-reconciler/src/ReactFiberThrow.new.js#L312
Error BoundaryとSuspenseめっちゃ似てる Suspense=PromiseのみをcatchするBoundary さいごに https://github.com/facebook/react/blob/main/packages/react-reconciler/src/ReactFiberThrow.new.js#L312
Error BoundaryとSuspenseめっちゃ似てる Suspense=PromiseのみをcatchするBoundary 宣言的にUIを構築するには、その対象を Reactの管理下に置く必要がある • state: 宣言的に書ける • ref:
宣言的に書けない さいごに https://github.com/facebook/react/blob/main/packages/react-reconciler/src/ReactFiberThrow.new.js#L312
Error BoundaryとSuspenseめっちゃ似てる Suspense=PromiseのみをcatchするBoundary 宣言的にUIを構築するには、その対象を Reactの管理下に置く必要がある • state: 宣言的に書ける • ref:
宣言的に書けない 宣言的に非同期処理扱うために、 PromiseをReactにthrowしている さいごに https://github.com/facebook/react/blob/main/packages/react-reconciler/src/ReactFiberThrow.new.js#L312
Error BoundaryとSuspenseめっちゃ似てる Suspense=PromiseのみをcatchするBoundary 宣言的にUIを構築するには、その対象を Reactの管理下に置く必要がある • state: 宣言的に書ける • ref:
宣言的に書けない 宣言的に非同期処理扱うために、 PromiseをReactにthrowしている Promiseをthrowすることへの違和感が減る (?) さいごに https://github.com/facebook/react/blob/main/packages/react-reconciler/src/ReactFiberThrow.new.js#L312
まとめ
まとめ • Suspenseを使うと非同期処理が宣言的に書ける • Suspenseだと非同期処理のエラーもError Boundaryで宣言 的にエラーハンドリングできる • SuspenseはPromiseのみをcatchするBoundary
まとめ • Suspenseを使うと非同期処理が宣言的に書ける • Suspenseだと非同期処理のエラーもError Boundaryで宣言 的にエラーハンドリングできる • SuspenseはPromiseのみをcatchするBoundary SuspenseとError
Boundaryでより宣言的なUI開発を!
ありがとうございました!