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
540
ローコードSaaSのUXを向上させるためのTypeScript
taro28
2
1.2k
GraphQLをServer Componentsで使いたい
taro28
8
3.2k
Sequenceを理解する
taro28
1
310
propsのバケツリレー対策でGlobal_Stateを使うその前に
taro28
12
5k
状態ってなに?🙃
taro28
2
600
一口目から美味しいReactのスルメ本🦑
taro28
3
1.6k
T-falってすごい【社内LT】
taro28
1
370
Other Decks in Programming
See All in Programming
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
190
ノイジーネイバー問題を解決する 公平なキューイング
occhi
0
120
CSC307 Lecture 08
javiergs
PRO
0
690
AIエージェントのキホンから学ぶ「エージェンティックコーディング」実践入門
masahiro_nishimi
7
1.1k
Claude Codeと2つの巻き戻し戦略 / Two Rewind Strategies with Claude Code
fruitriin
0
180
要求定義・仕様記述・設計・検証の手引き - 理論から学ぶ明確で統一された成果物定義
orgachem
PRO
1
360
atmaCup #23でAIコーディングを活用した話
ml_bear
4
610
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
1
350
SourceGeneratorのススメ
htkym
0
610
2026年は Rust 置き換えが流行る! / 20260220-niigata-5min-tech
girigiribauer
0
130
AIコーディングの理想と現実 2026 | AI Coding: Expectations vs. Reality 2026
tomohisa
0
340
Geminiの機能を調べ尽くしてみた
naruyoshimi
0
110
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
698
190k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Exploring anti-patterns in Rails
aemeredith
2
270
Ethics towards AI in product and experience design
skipperchong
2
210
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Balancing Empowerment & Direction
lara
5
910
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Accessibility Awareness
sabderemane
0
67
Imperfection Machines: The Place of Print at Facebook
scottboms
269
14k
Information Architects: The Missing Link in Design Systems
soysaucechin
0
800
The Invisible Side of Design
smashingmag
302
51k
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開発を!
ありがとうございました!