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
Anti Hook
Search
Mark24
March 25, 2020
Programming
85
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Anti Hook
You may not need Hook
Mark24
March 25, 2020
More Decks by Mark24
See All by Mark24
DSL分享
mark24code
0
130
Rails初探
mark24code
0
52
Git恢复Force Push内容
mark24code
0
77
Other Decks in Programming
See All in Programming
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
55
36k
PyO3 で既存 Python 評価器を Rust core 化する ー wasm-bindgen でブラウザにも配るための設計
kdash
1
560
kubernetes コンポーネント開発入門 / 新卒N年目の勉強会&交流会!〜〇〇への誘い〜 #n_study
mazrean
0
180
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
5
1.3k
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
230
Claude Codeを組織的に動かして月400PRを実現した話
happy_ryo
0
260
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
180
数年滞っていたダークモード対応をおよそ2週間で完了させる
chigichan24
0
690
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
110
Family mrubyの進捗
kishima
1
100
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
760
Laravelのアプリケーションをどこにデプロイするか #ツナギメオフライン.9
akase244
0
120
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Side Projects
sachag
455
43k
Ruling the World: When Life Gets Gamed
codingconduct
0
330
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
430
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.6k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
410
Utilizing Notion as your number one productivity tool
mfonobong
4
580
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
65k
Test your architecture with Archunit
thirion
2
2.4k
We Have a Design System, Now What?
morganepeng
55
8.3k
A Soul's Torment
seathinner
7
3.6k
Transcript
你可能不需要Hook @Mark 2020.03.25
Hook 是什么?
https://zh-hans.reactjs.org/docs/hooks-reference.html
None
useState模型 • 官⽅⽤法: https://codesandbox.io/s/react-hooks- demo-1-7lw3p • 简单实现:https://codesandbox.io/s/react-hooks- demo-2-4c92p
None
Flow&内部
None
实现 • 内部链表(理解成列表) ——> 单独储存 • 按顺序读取值,按顺序执⾏ hook (不断地rerender,状态 机)
• 触发更新,和 setState⼀样最后都进⼊ Fiber更新队列
优点 • 跨⽣命周期复⽤代码 • 你可以⾃定义Hook
本质:复⽤代码的尝试 PS:以函数为载体的复⽤
理论上
None
实际上
None
问题0:设计
违反单⼀,KISS原则
None
如果需要你学习很久,那其实 是很是失败的设计。 每个⼈理解的不⼀样。
问题1:可视化阅读
1.Class的⽣命周期,字⾯量划分被打 破。视觉阅读结束。 你需要去看每个函数式组件。 2.他并没有明显区分。
问题2:⼼智负担
1.Hook⾃⼰⽐较难理解 2.useEffect的官⽅介绍就要 70min
1.以前的组件⼼智模型⼀致 现在你需要知道Hook做了什么 你必须熟悉每⼀个Hook。 2.他⽤了什么state,还是context,他⽤了 effect还是什么 3.还有他⾃⼰写的Hook——毕竟这是跨⽣命周 期⾥⾯的复⽤。
问题3:滥⽤?闭包
1.你的代码会收获⼀⼤堆罗列在⼀起 的函 数。 2.看起来像极了C语⾔ 3.OO本身就是为了简化问题,这次反向倒⻋ 4.闭包是复杂的,潜在的内存泄漏
问题4:开发体验差
1.闭包,意味着你可能出现内存泄漏⽽ 不⾃制。轻轻松松写出有问题的代码。 2.⽆限循环之殇:写错了就要重启浏览 器。体验极差
问题5:代码真的少 了?
1.每⼀步都要追踪依赖,优化性 能,真的很麻烦 2.碎⽚化的useState未必真的好
你的开悟瞬间
⼀苇渡江 开悟时刻 技术的平衡游戏 平衡是编程⾥⾯的艺术
Hook也许不是为你准 备的
可能适合组件库 • 纯粹,⼀致性的组件。可以极⼤地发挥其复⽤性。 • 因为纯粹所以写不复杂。每个组件是健壮⽽⾼效的。
也许不适合业务
1.⾸先他不纯粹 2.易变化 3.琐碎且容⻓的业务逻辑 4.相似⼜不相关——并不能很好被复⽤ 5.可读性是业务代码中最重要的
理性拥抱新技术 新技术!==好技术 好技术===经得起时间考验的技术
参考资料 • 《useEffect 完整指南》https://overreacted.io/zh-hans/a- complete-guide-to-useeffect/ • https://github.com/brickspert/blog/issues/26 • https://github.com/donavon/hook-flow