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
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
270
AIエージェントが動かないときの原因とその対処
sadnessojisan
2
170
React のルーター事情
sadnessojisan
1
660
PHPこそ OpenTelemetry が嬉しい
sadnessojisan
2
500
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 時代の Azure エンジニアリング ~ 私たちは何を磨き、何を任せるのか ~
chack411
1
480
並行性の問題を防げ!実践トランザクション入門
occhi
0
280
あなたの知らないAmazon VPC Route Server/Amazon VPC Route Server you don't know about
masakiokuda
2
220
おそらく日本で唯一のDevRelインターン生として
husengs7
0
170
分析の民主化を支えるメタデータ整備の実践
kakehashi
PRO
0
170
IR Today: Theory, Practice, and Agents
dtunkelang
0
350
HacobuにおけるFDEとは/登壇資料(戸井田 裕貴)
hacobu
PRO
1
780
[2026 Oracle Technical Deep Dive] AI時代のアプリケーションを支えるCloud NativeとJava EEモダナイゼーション (2026年9月17日開催)
oracle4engineer
PRO
0
120
転んだ話が背中を預ける理由になる / A_story_about_falling_down_gives_you_a_reason_to_watch_each_other_s_backs.pdf
pauli
3
270
Google Cloud Next Tokyo 26登壇時のスクリプト
recruitengineers
PRO
0
200
FactoryBotアンチパターン / factory_bot anti-patterns
toshimaru
0
140
[2026 Oracle Technical Deep Dive] VMwareワークロードのOCI移行実践ガイド - OCVSでの継続利用からOCIコンピュートへの移行まで (2026年9月17日開催)
oracle4engineer
PRO
0
130
Featured
See All Featured
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
390
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
590
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.6k
Designing Powerful Visuals for Engaging Learning
tmiket
1
590
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Typedesign – Prime Four
hannesfritz
42
3.2k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
370
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.1k
Building Applications with DynamoDB
mza
96
7.2k
From π to Pie charts
rasagy
1
390
HDC tutorial
michielstock
2
940
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 になりそう