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
preactの仕組みを理解する軽量版教育用preactを作ってる話
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
sadnessOjisan
November 05, 2020
Technology
3.4k
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
preactの仕組みを理解する軽量版教育用preactを作ってる話
preact を読みやすいように色々削りました
sadnessOjisan
November 05, 2020
More Decks by sadnessOjisan
See All by sadnessOjisan
Cloudflare Workers で Rust 選ぶ理由 is 何?
sadnessojisan
1
96
AIエージェントが動かないときの原因とその対処
sadnessojisan
2
140
React のルーター事情
sadnessojisan
1
640
PHPこそ OpenTelemetry が嬉しい
sadnessojisan
2
460
TypeScript、上達の瞬間
sadnessojisan
53
19k
フロントエンド・オブザーバビリティを支える要素技術を学ぼう
sadnessojisan
2
990
疎通2024
sadnessojisan
5
1.7k
BasicBasic認証
sadnessojisan
5
4.9k
ISUCON入門以前_ISUNARABE_LT#1
sadnessojisan
21
6.6k
Other Decks in Technology
See All in Technology
“それは自分の仕事じゃない"を越えて行け
yuukiyo
1
520
どこまでAIに任せるか 〜確率論と決定論の境界決定〜
shukob
0
450
ダッシュボード"開発"について 〜使われるダッシュボードのつくりかた〜
kimichan
0
200
現場との対話から始める “作る前に問い直す”業務改善
mochico50
1
230
文字起こし基盤の信頼性
abnoumaru
0
120
VPCセキュリティ対応の最新事情
nagisa53
1
290
Webアプリ認証の全体像 / The Big Picture of Web App Authentication
kitano_yuichi
1
430
AIが当たり前の組織で エンジニアはどう育つか
nishihira
1
970
AIエージェントがあれば技術書なんてすぐ書けるでしょ→無理でした
watany
2
280
Aurora MySQL 8.4リリース! Rubyistが備えること / what-rubyist-should-prepare-for-aurora-mysql-8-4
fkmy
0
750
AIとハーネスで育てるトランスコンパイラ / 20260722 Yasushi Katayama
shift_evolve
PRO
3
780
AI時代の開発生産性を捉え直す — 経営と現場をつなぐ「開発組織のオブザーバビリティ」— / AI Dev Ex Conference 2026
tkyowa
1
1.3k
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
560
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
410
Into the Great Unknown - MozCon
thekraken
41
2.6k
How GitHub (no longer) Works
holman
316
150k
Testing 201, or: Great Expectations
jmmastey
46
8.2k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
510
Transcript
仮想DOMの仕組みを理解する 軽量版教育用 preact 〜俺の React、oreactを作った話〜 Yuta Ide (@sadnessOjisan) #new_style_study
自粛期間にしていたこと ❑ preact 読んでた https://blog.ojisan.io/preact-reading
preact とは ❑ 軽量版React, Reactのサブセット ❑ React と異なる実装だが、同一の機能・同一のインターフェースを 持つ ❑
後発のライブラリであり、比較的読みやすい
2分でわかる preact
用語の整理 ❑ Element <div>hey</div> みたいなの ❑ Component ClasComponent, Functional Component.
Element と区別しよう ❑ 仮想DOM DOMをJSのオブジェクトで表現したもの ❑ VNode preact における仮想DOMの表現 https://blog.ojisan.io/react-component-words
preact の責務 ❑ 仮想DOMからDOMツリーを構築 ❑ 新旧の仮想DOMを比較して差分をDOMに反映
Node Node Node Node Node Node diff(dom, newVNode, oldVNode)
主要な関数 diff 差分比較の起点。下記2つの関数を呼ぶ diffChildren Element + Component の VNode の
新旧children を比較して、DOM ツリーの追加・削除を行う。diff を再帰的に呼ぶ diffElementNodes Element の VNode の 新旧 props を比較して、DOM Node のプロパ ティを更新する。diffChildren を再帰的に呼ぶ
どのように diff が取られるか
Component Component Component Element Element Element Element Element props 更新
Component Component Component Element Element Element Element Element props 更新
diff 更新内容の newVNode が diff に渡されて呼ばれる
Component Component Component Element Element Element Element Element props 更新
diffChildren diff 子要素を持つコンポーネ ントなので、ツリー自体 に増減があるか確かめる
Component Component Component Element Element Element Element Element props 更新
diffChildren diff Element 子要素に対して diff を取る
Component Component Component Element Element Element Element Element props 更新
diffChildren diff Element diffElementNodes 子を持たないNodeに対して 、そのNode自体の値を更新 する
Component Component VNode Component Element Element Element Element Element props
更新 function diffChildren diff Element diffElementNodes diff を再帰的に呼ぶことで、 ツリーを深く辿ることができる
Props の更新で diff が発火する仕組み setState
setState renderComponent diff setState が呼ばれるとdiff が呼ばれるため 再レンダリングが起きる 次の状態をComponentの _nextStateに保存 diff
を呼び出す _nextState を取り出す
setState renderComponent diff setState が呼ばれるとdiff が呼ばれるため 再レンダリングが起きる props が変わったら再レンダリング ↓
propsを監視しているわけではない!
setState が呼ぶ方法 lifecycle Eventハンドラからも呼び出せますが、preact成分は薄いので省きます
diff diffChildren diffElementNodes preact が diff中、diff後に実行してくれる commitRoot ライフサイクルの実行 renderQueueにライフサイ クルイベントの追加
renderQueueのイベントを 実行
preact とは ❑ 軽量版React, Reactのサブセット ❑ React と異なる実装だが、同一の機能・同一のインターフェースを 持つ ❑
後発のライブラリであり、読みやすい
本当に読みやすい? ❑(比較的)読みやすい ❑ランタイムにおける最適化や行数の節約が意識されていたり、多少の 可読性は失われている ❑変数の再代入 ❑オブジェクトの破壊 ❑型が合わない ❑クラスを使わない ❑適当すぎる変数名 (i,
j に Vnode を代入) ❑tmp にいろいろなもの渡して使いまわしてる ❑相互再帰 ❑条件節での代入 ❑条件節での副作用 ❑ For me 読みにくい!
というわけで、 読みやすい教育用 preact を作る
俺でも読める preact oreact https://github.com/sadnessOjisan/oreact
必要最低限の機能だけをサポート ❑ State ❑ Props ❑ ライフサイクル ❑ componentDidMount ❑
componentWillReceive ❑ componentWillUnMount ❑ スタイリング ❑ イベントハンドリング これだけあればなんでも(泥臭く) 作れる!! Let’s drilling props!
消した機能 ❑ Context ❑ Hooks ❑ Ref ❑ svg ❑
dangerouslySetInnerHTML ❑ setState への関数渡し ❑ 一部ライフサイクル ❑ SSRサポート
引数をオブジェクトで渡す ❑ 引数の数が多い ❑ 再帰的に呼ぶ関数がいくつかあり、どの状態になった引数が渡され るかを知りたい
型をつける ❑ 本家は JSDoc に型を書いている ❑ それをもとにTSで書き直す ❑ 型が合わない・・・ ❑
一旦は後回しにしてドキュメントだけ充実させる
サンドボックスの作成 ❑ モノレポにした ❑ oreact を使ってアプリを作れるか確かめれる環境を作成 ❑ oreact 自体に console.log
をしかけると、再帰的に呼ばれる diff の引数 を除き見れたりして挙動の理解がしやすい ❑ ライブラリ部分だけを publish もできる
https://sadnessojisan.github.io/oreact/
今後の目標 ❑ 変数の再代入を直したい ❑ 意味を持たない変数名に名前を適切な名前を与えたい ❑ 型を合わせたい これに対応しようとすると、結局は書き直しに なりそう。書き直したものは preact
と呼べる のか? 本当の意味で 俺の react になりそう