So adding class lifecycle events to functional components? Why not just use a class?
The beauty of the functional components is that immediately you know there's ZERO state in here. It's just rendering markup based on input.
Now there's more to look out for given this hook thing. I don't like it.
Immediate explorations I've seen have dramatically cut the number of lines for implementation. One example I saw cut 130 lines down to 15. These APIs also enable new behaviors not achievable with classes (extracting reusable stateful logic), as well as letting you consolidate related logic so it's not all intermingled with unrelated functionality.
Software developer with 5+ years of experience. Currently focused on web apps and web games. Check my site https://frontendgamedev.com/ where you can find useful information about web games.
To be honest you can extract reusable state logic by using abstract classes for example but rather for some small logic and I wouldn't go too far with that because it can quickly end up with ugly non-readable composition.
Also, I hate classes because you can quickly end up with a mess (lots of unrelated things in componentDidMount, componentWillUnmount, having the same state in multiple classes etc). Hooks are a really great improvement (for readability and composition).
Inheriting from a custom class is considered an antipattern in React, so while it works on a technical level I wouldn't say it's a viable option. All the downsides of mixins without any benefits over other ways of composing functionality.
Preact - Next.js - Deno - SSR - SSG 💻
Open source 📂
Machine learning 🤖
Books 📖
Sport 🏃♂️
Nature 🌱
Focus on being useful. I contribute AMAP to the OSS community. Love learning by doing.
Hooks are a beauty alternative to renderprops or hoc. Reusing logic without JSX in the middle. Of course they don't reinvent another lifecycle and they reuse the existent one.
Components are now more atomic, with a separation of concerns.
At the beginning I didn't like so much until I realized that is possible to generate a custom hooks. IMO here is where is all the power.
So adding class lifecycle events to functional components? Why not just use a class?
The beauty of the functional components is that immediately you know there's ZERO state in here. It's just rendering markup based on input.
Now there's more to look out for given this
hook
thing. I don't like it.I do agree here for
useState
. I can see some of the other hooks being very useful though. EspeciallyuseEffect
anduseCallback
...Immediate explorations I've seen have dramatically cut the number of lines for implementation. One example I saw cut 130 lines down to 15. These APIs also enable new behaviors not achievable with classes (extracting reusable stateful logic), as well as letting you consolidate related logic so it's not all intermingled with unrelated functionality.
To be honest you can extract reusable state logic by using abstract classes for example but rather for some small logic and I wouldn't go too far with that because it can quickly end up with ugly non-readable composition.
Also, I hate classes because you can quickly end up with a mess (lots of unrelated things in componentDidMount, componentWillUnmount, having the same state in multiple classes etc). Hooks are a really great improvement (for readability and composition).
Inheriting from a custom class is considered an antipattern in React, so while it works on a technical level I wouldn't say it's a viable option. All the downsides of mixins without any benefits over other ways of composing functionality.
Hooks are a beauty alternative to renderprops or hoc. Reusing logic without JSX in the middle. Of course they don't reinvent another lifecycle and they reuse the existent one.
Components are now more atomic, with a separation of concerns.
At the beginning I didn't like so much until I realized that is possible to generate a custom hooks. IMO here is where is all the power.
Agreed, I never felt a need for this.