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
taro
September 08, 2022
Programming
9
7.2k
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
10
3k
ローコードサービスの進化のためのモノレポ移行
taro28
2
480
ローコードSaaSのUXを向上させるためのTypeScript
taro28
2
1.1k
GraphQLをServer Componentsで使いたい
taro28
8
3.1k
Sequenceを理解する
taro28
1
280
propsのバケツリレー対策でGlobal_Stateを使うその前に
taro28
12
4.8k
状態ってなに?🙃
taro28
2
560
一口目から美味しいReactのスルメ本🦑
taro28
3
1.5k
T-falってすごい【社内LT】
taro28
1
330
Other Decks in Programming
See All in Programming
私の後悔をAWS DMSで解決した話
hiramax
4
210
個人軟體時代
ethanhuang13
0
320
JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #devio2025
dafujii
1
530
go test -json そして testing.T.Attr / Kyoto.go #63
utgwkk
3
310
概念モデル→論理モデルで気をつけていること
sunnyone
2
260
複雑なドメインに挑む.pdf
yukisakai1225
5
1.2k
testingを眺める
matumoto
1
140
Navigation 2 を 3 に移行する(予定)ためにやったこと
yokomii
0
270
FindyにおけるTakumi活用と脆弱性管理のこれから
rvirus0817
0
520
Performance for Conversion! 分散トレーシングでボトルネックを 特定せよ
inetand
0
880
Cache Me If You Can
ryunen344
2
1.5k
Android端末で実現するオンデバイスLLM 2025
masayukisuda
1
150
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
Documentation Writing (for coders)
carmenintech
74
5k
Designing for Performance
lara
610
69k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
530
Code Review Best Practice
trishagee
70
19k
Embracing the Ebb and Flow
colly
87
4.8k
Into the Great Unknown - MozCon
thekraken
40
2k
A better future with KSS
kneath
239
17k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
810
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開発を!
ありがとうございました!