Hacker News new | ask | show | jobs
by ducharmdev 1777 days ago
Just curious, but in your opinion, how does the composition API improve on React hooks?

I used Vue 2 for some projects a year or two ago, and recently have been comparing React hooks & Vue's composition API. Composition is definitely better than mixins, but as a new React user, hooks are feeling more intuitive right off the bat. With Vue 3, I feel like I have to decide for every component whether to stick with the options API or use the setup method, or end up with some awkward in-between.

I was always a big fan of Vue though; I'd appreciate any insight you can give on the Vue 3 additions.

2 comments

> Just curious, but in your opinion, how does the composition API improve on React hooks?

I'm a fan of how Vue's composition API executes exactly once during a component lifetime (during `setup()`), whereas React hooks get set up again on every render. I find it I find Vue's approach easier to mentally model, and thus easier to write correct code (especially when doing something complicated where state changes / effect executions trigger each other). Since `setup()` is only run once, I can also do things like store non-reactive state in closure variables without wrapping those variables in hooks.

I think this puts it in perspective more; if I understand what you're saying correctly, then setup essentially gives you more fine-grained control over Vue's reactivity system, as opposed to just doing it all for you when a component is created. Which allows you to more easily pull out & consolidate reusable logic as needed.

It is kinda nice making reactive state really explicit like that; I like Svelte's approach for similar reasons.

There are many points. The main point for me is dependency tracking.

React hooks require manual dependency tracking and mistakes in this can lead hard to identify bugs

Vue has automatic dependency tracking, so that entire class of issues do not occur.

THIS. People focus so much on templates vs jsx and most forget this is the biggest difference, and a very important one. I think people discarding Vue because they don't like templates just don't understand this difference.