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 の非同期処理について考える
Search
kotaro takahashi
December 19, 2019
Programming
1
740
これからの React の非同期処理について考える
スタートアップ×React LT大会 Coral Developers Nightにて、10分間のLTで登壇。
Reactと非同期処理から連想出来るトピックについて早口オタク喋りした。
kotaro takahashi
December 19, 2019
Tweet
Share
More Decks by kotaro takahashi
See All by kotaro takahashi
もしもエンジニアが Inspiredを読んだら ~PdMを兼任した半年間の奮闘記~
k4h4shi
0
79
Other Decks in Programming
See All in Programming
FindyにおけるTakumi活用と脆弱性管理のこれから
rvirus0817
0
410
HTMLの品質ってなんだっけ? “HTMLクライテリア”の設計と実践
unachang113
3
2.1k
CloudflareのChat Agent Starter Kitで簡単!AIチャットボット構築
syumai
2
400
Go言語での実装を通して学ぶLLMファインチューニングの仕組み / fukuokago22-llm-peft
monochromegane
0
110
Claude Codeで挑むOSSコントリビュート
eycjur
0
190
Protocol Buffersの型を超えて拡張性を得る / Beyond Protocol Buffers Types Achieving Extensibility
linyows
0
110
「手軽で便利」に潜む罠。 Popover API を WCAG 2.2の視点で安全に使うには
taitotnk
0
680
Laravel Boost 超入門
fire_arlo
2
200
AI時代のUIはどこへ行く?
yusukebe
13
7.8k
【第4回】関東Kaggler会「Kaggleは執筆に役立つ」
mipypf
0
1k
RDoc meets YARD
okuramasafumi
4
160
Microsoft Orleans, Daprのアクターモデルを使い効率的に開発、デプロイを行うためのSekibanの試行錯誤 / Sekiban: Exploring Efficient Development and Deployment with Microsoft Orleans and Dapr Actor Models
tomohisa
0
230
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
330
21k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Visualization
eitanlees
148
16k
Building a Modern Day E-commerce SEO Strategy
aleyda
43
7.5k
Making Projects Easy
brettharned
117
6.4k
Gamification - CAS2011
davidbonilla
81
5.4k
Site-Speed That Sticks
csswizardry
10
810
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Automating Front-end Workflow
addyosmani
1370
200k
Designing for humans not robots
tammielis
253
25k
Transcript
これからの React の非同期処理について考える スタートアップ×React LT 大会 Coral Developers Night
kotaro.t ID: @k4h4shi 所属: 株式会社Handii ロール: フロントエンドエンジニ ア
はじめに
非同期処理はどこに書く?
非同期処理関数() { 非同期処理の開始アクションの発行; try { 非同期処理の呼び出し; 非同期処理の完了アクションの発行; } catch (e) {
非同期処理の失敗アクションの発行; } }
redux-saga
function* asyncTask() { yield put(asyncActions.start()); try { const result = yeild call(asyncFunction); yield put(asyncActions.done(result)); } catch (e) { yield put(asyncActions.failed(e)) } }
Saga の良い点
モック要らずで手続きのテストが容易 コンポーネントとタスクが疎に出来る API が豊富(delay, retry, cancel, throttle, debounce)
Saga の悪い点
TS の型推論があんま効かない 見慣れないジェネレータ記法でロックインされる 影響と副作用を一手に引き受けるのでカオスになりやすい
Saga は便利だが、扱いが難しい道具
None
そんな時...
React 16.8 で React Hooks がリリースされた
もう Hooks でいいのでは...?
Hooks での非同期処理について考えた
const useDoAsyncOperation = () => { const dispatch = useDispatch(); const doAsyncOperation = async () => { dispatch(asyncActions.start()); try { const result = await asyncFunction(); dispatch(asyncActions.done(result)); } catch (e) { dispatch(asyncActions.failed(e)) }
} return doAsyncOperation; }
const component = () => { const doAsyncOperations = useDoAsyncOperation(); return ( <button onClick={doAsyncOperations}>do asyncOperation</button> ) }
None
これまでと今のReact の非同期処理について考えた...
これからの React の非同期処理について考える
Suspense for Data Fetching (Experimental)
const ProfilePage = React.lazy(() => import('./ProfilePage')); // Lazy-loaded // Show a spinner while the profile is loading <Suspense fallback={<Spinner />}> <ProfilePage /> </Suspense>
const resource = fetchProfileData(); function ProfilePage() { return ( <Suspense fallback={<h1>Loading profile...</h1>}> <ProfileDetails /> <Suspense fallback={<h1>Loading posts...</h1>}> <ProfileTimeline /> </Suspense> </Suspense>
); } function ProfileDetails() { const user = resource.user.read(); return <h1>{user.name}</h1>; } function ProfileTimeline() { const posts = resource.posts.read(); return ( <ul> {posts.map(post => ( <li key={post.id}>{post.text}</li> ))}
None
宣言的に記述する事でReact による最適化の恩恵が受けら れる
さいごに
ご清聴ありがとうございました