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
290
AIエージェントが動かないときの原因とその対処
sadnessojisan
2
170
React のルーター事情
sadnessojisan
1
670
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
FOSS4Gを活用した自治体業務DXの試み
470715
0
140
転んだ話が背中を預ける理由になる / A_story_about_falling_down_gives_you_a_reason_to_watch_each_other_s_backs.pdf
pauli
3
340
1万名の社員が使う認証基盤で どう信頼性を担保するか?
kairim0
0
220
Loop Engineering これくらいでいい
mikanichinose
0
140
生成AIと進める探索的データ分析(2026.10.03 第122回Tokyo.R勉強会)
tatamiya
2
1.5k
分析の民主化を支えるメタデータ整備の実践
kakehashi
PRO
4
1.2k
Kiro Crewをはじめよう
kizawa2020
1
110
味覇で作るパスタが美味い - 20261001_yabaubuki.dev
livesense
PRO
0
110
AIがコードを書く時代に、エンジニアは何で自分の価値を証明するのか / In an era where AI writes code how do engineers prove their value
carta_engineering
1
670
並行性の問題を防げ!実践トランザクション入門
occhi
0
280
AI駆動開発の時代になったのでトヨタ生産方式から見直す
terurou
6
5.5k
会社説明資料
persolserverworks
0
9.6k
Featured
See All Featured
Joys of Absence: A Defence of Solitary Play
codingconduct
1
540
A Tale of Four Properties
chriscoyier
163
24k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
440
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
840
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Balancing Empowerment & Direction
lara
6
1.3k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
540
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
350
How Software Deployment tools have changed in the past 20 years
geshan
2
35k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
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 になりそう