Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Class based components are easy to pick up, but they don't scale. They're really hard to refactor, and make it hard to reuse behavior. If you have a simple app though, they'll do fine.

Often with hooks, you'll have to think differently in order to figure out how to accomplish what you want to do more functionally. That up-front cost ends up paying back later because you'll rarely need to touch it, and it's dead-simple to reuse somewhere else. This pattern from the blog post of "fetching state from within a setTimeout" is rare, but your example of accessing state from within a callback should be manageable.

Here's the thing: even though the example from the blog post is strange, the advantage of hooks is that you can pull that pattern out into a hook! So instead of `useState`, you'd call your hook that returns the reactive value, a setter, and a getter that uses the ref under the hood. Then you can use that getter in the `setTimeout`, and you never have to think about this problem again when you have to do this for any other piece of state!



“They don’t scale”

They scale great. Decorators are incredibly underrated for code reuse.


You can't compose behaviors effectively with class based components, and the fact that you only have one of each lifecycle method means that they become an absolute mess as you scale with complexity. Each behavior gets split up across multiple lifecycle methods, mixed in with every other behavior. With hooks, you can encapsulate each behavior into a single hook function that handles its complete lifecycle, and then trivially use it anywhere else.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: