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.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
エージェント開発初心者の僕がエージェントを作った話と今後やりたいこと
thasu0123
0
160
Gemini for developers
meteatamel
0
120
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
300
AI主導でFastAPIのWebサービスを作るときに 人間が構造化すべき境界線
okajun35
0
270
Ruby x Terminal
a_matsuda
3
190
Premier Disciplin for Micro Frontends Multi Version/ Framework Scenarios @OOP 2026, Munic
manfredsteyer
PRO
0
190
NOT A HOTEL - 建築や人と融合し、自由を創り出すソフトウェア
not_a_hokuts
2
460
Apache Iceberg V3 and migration to V3
tomtanaka
0
220
AIプロダクト時代のQAエンジニアに求められること
imtnd
1
480
The Ralph Wiggum Loop: First Principles of Autonomous Development
sembayui
0
3.7k
日本だけで解禁されているアプリ起動の方法
ryunakayama
0
360
AIによる開発の民主化を支える コンテキスト管理のこれまでとこれから
mulyu
3
2k
Featured
See All Featured
Facilitating Awesome Meetings
lara
57
6.8k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
59
50k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
59
Code Reviewing Like a Champion
maltzj
527
40k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
850
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
320
Everyday Curiosity
cassininazir
0
140
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
170
Technical Leadership for Architectural Decision Making
baasie
2
270
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.9k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
210
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開発を!
ありがとうございました!