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
Graviton と Nitro と私
maroon1st
0
160
Developing static sites with Ruby
okuramasafumi
1
350
ThorVG Viewer In VS Code
nors
0
600
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
130
perlをWebAssembly上で動かすと何が嬉しいの??? / Where does Perl-on-Wasm actually make sense?
mackee
0
290
Grafana:建立系統全知視角的捷徑
blueswen
0
280
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
250
大規模Cloud Native環境におけるFalcoの運用
owlinux1000
0
240
ゆくKotlin くるRust
exoego
1
190
チームをチームにするEM
hitode909
0
440
Deno Tunnel を使ってみた話
kamekyame
0
310
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 2
philipschwarz
PRO
0
130
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
It's Worth the Effort
3n
187
29k
Crafting Experiences
bethany
0
26
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.6k
Making Projects Easy
brettharned
120
6.5k
The SEO identity crisis: Don't let AI make you average
varn
0
47
A Tale of Four Properties
chriscoyier
162
23k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.2k
Design in an AI World
tapps
0
110
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
0
1.8k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.3k
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 による最適化の恩恵が受けら れる
さいごに
ご清聴ありがとうございました