これからの React の非同期処理について考える
by
kotaro takahashi
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
これからの React の非同期処理について考える スタートアップ×React LT 大会 Coral Developers Night
Slide 2
Slide 2 text
kotaro.t ID: @k4h4shi 所属: 株式会社Handii ロール: フロントエンドエンジニ ア
Slide 3
Slide 3 text
はじめに
Slide 4
Slide 4 text
非同期処理はどこに書く?
Slide 5
Slide 5 text
非同期処理関数() { 非同期処理の開始アクションの発行; try { 非同期処理の呼び出し; 非同期処理の完了アクションの発行; } catch (e) { 非同期処理の失敗アクションの発行; } }
Slide 6
Slide 6 text
redux-saga
Slide 7
Slide 7 text
function* asyncTask() { yield put(asyncActions.start()); try { const result = yeild call(asyncFunction); yield put(asyncActions.done(result)); } catch (e) { yield put(asyncActions.failed(e)) } }
Slide 8
Slide 8 text
Saga の良い点
Slide 9
Slide 9 text
モック要らずで手続きのテストが容易 コンポーネントとタスクが疎に出来る API が豊富(delay, retry, cancel, throttle, debounce)
Slide 10
Slide 10 text
Saga の悪い点
Slide 11
Slide 11 text
TS の型推論があんま効かない 見慣れないジェネレータ記法でロックインされる 影響と副作用を一手に引き受けるのでカオスになりやすい
Slide 12
Slide 12 text
Saga は便利だが、扱いが難しい道具
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
そんな時...
Slide 15
Slide 15 text
React 16.8 で React Hooks がリリースされた
Slide 16
Slide 16 text
もう Hooks でいいのでは...?
Slide 17
Slide 17 text
Hooks での非同期処理について考えた
Slide 18
Slide 18 text
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; }
Slide 19
Slide 19 text
const component = () => { const doAsyncOperations = useDoAsyncOperation(); return ( do asyncOperation ) }
Slide 20
Slide 20 text
No content
Slide 21
Slide 21 text
これまでと今のReact の非同期処理について考えた...
Slide 22
Slide 22 text
これからの React の非同期処理について考える
Slide 23
Slide 23 text
Suspense for Data Fetching (Experimental)
Slide 24
Slide 24 text
const ProfilePage = React.lazy(() => import('./ProfilePage')); // Lazy-loaded // Show a spinner while the profile is loading }>
Slide 25
Slide 25 text
const resource = fetchProfileData(); function ProfilePage() { return ( Loading profile...}> Loading posts...}> ); } function ProfileDetails() { const user = resource.user.read(); return
{user.name}
; } function ProfileTimeline() { const posts = resource.posts.read(); return (
{posts.map(post => (
{post.text}
))}
Slide 26
Slide 26 text
No content
Slide 27
Slide 27 text
宣言的に記述する事でReact による最適化の恩恵が受けら れる
Slide 28
Slide 28 text
さいごに
Slide 29
Slide 29 text
ご清聴ありがとうございました