可以把 React hook 理解为 @decorator(装饰器) 的作用吗?

2019-06-25 23:29:12 +08:00
 lisisi
都在不破坏 /不重写原函数的基础上,实现了新的功能。hook 和 @decorator 原理上有区别吗?
6005 次点击
所在节点    JavaScript
7 条回复
ech0x
2019-06-26 06:46:17 +08:00
“ Hooks are functions that let you “ hook into ” React state and lifecycle features from function components. Hooks don ’ t work inside classes — they let you use React without classes. (We don ’ t recommend rewriting your existing components overnight but you can start using Hooks in the new ones if you ’ d like.)”
我不懂 JavaScript,不过看这个描述应该是不一样的 hook 跟 react 生命周期有关。
python 的装饰器只是高级函数的语法糖而已。
jason94
2019-06-26 07:57:09 +08:00
两个完全不一样的东西~~建议补一补基础,先把两者的定义弄明白了。
byenow
2019-06-26 13:22:16 +08:00
@decorator 是对 Class 的修改,基于 Class Components,React Hooks 基于 Function Components
bestie
2019-06-27 09:44:08 +08:00
decorator 更类似于高阶函数( HOC )
azh7138m
2019-07-09 15:49:21 +08:00
完全不一样的东西,建议阅读 https://github.com/acdlite/recompose
decorator 怎么就 hoc 了。。。
ahonn
2019-07-21 11:15:57 +08:00
<amp-youtube data-videoid="KJP1E-Y-xyo" layout="responsive" width="480" height="270"></amp-youtube>看完这个你就懂了
kwrush
2019-07-26 20:43:21 +08:00
@bestie hoc 就是装饰器模式吧

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/577461

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX