Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
preactの仕組みを理解する軽量版教育用preactを作ってる話
Search
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
290
AIエージェントが動かないときの原因とその対処
sadnessojisan
2
170
React のルーター事情
sadnessojisan
1
670
PHPこそ OpenTelemetry が嬉しい
sadnessojisan
2
510
TypeScript、上達の瞬間
sadnessojisan
54
19k
フロントエンド・オブザーバビリティを支える要素技術を学ぼう
sadnessojisan
2
1k
疎通2024
sadnessojisan
5
1.7k
BasicBasic認証
sadnessojisan
5
5k
ISUCON入門以前_ISUNARABE_LT#1
sadnessojisan
21
6.8k
Other Decks in Technology
See All in Technology
AI時代に必要な脅威モデリング― 変化の速度に負けない理解とリスク判断
masakane55
5
1.3k
Execution in the Kingdom of Agents: Reflections on Abstraction and Complexity
bcantrill
0
940
おそらく日本で唯一のDevRelインターン生として
husengs7
0
170
オンラインゲームのシステム全体像 - コロプラ 2026年度 新卒研修
colopl
0
1.1k
脆弱性診断って、何をしているの? 〜弱点を見つけて、会社の改善につなげるまで〜
keeg
0
860
Deep Data Security 機能解説
oracle4engineer
PRO
2
770
転んだ話が背中を預ける理由になる / A_story_about_falling_down_gives_you_a_reason_to_watch_each_other_s_backs.pdf
pauli
3
350
実践!Cloud Run ハンズオン!! / Cloud Run handson
y0hgi
0
200
ハードウェアコンペでもAI駆動開発が進んでいる話
iotengineer22
1
680
「Docker Desktop不要の時代」は 来たのか?
ueponx
0
510
Jetson Orin Nanoとk3sとローカルLLMで音声文字起こしシステム作ってみた
y_sera15
0
120
SREでアラート疲れを 解決しよう!
kairim0
1
220
Featured
See All Featured
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
320
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
How to Talk to Developers About Accessibility
jct
2
570
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
30 Presentation Tips
portentint
PRO
1
430
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Speed Design
sergeychernyshev
33
2.2k
Fireside Chat
paigeccino
43
4.1k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
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 になりそう