7 ms·
I can't get over how amazing hooks are and how resistant I initially was to them. They encapsulate logic so well and do an amazing job being this isolated data
by Waterluvian 5y ago
I can't get over how amazing hooks are and how resistant I initially was to them.
They encapsulate logic so well and do an amazing job being this isolated data/event source.
I find that I reuse them far more than I reuse components.
- k__ 5y agoThey fit so well into React it's crazy. But they're also soooo idiosyncratic which makes them rather ugly on a general level.
- codeflo 5y agoThey also come with many restrictions and rules on correct and efficient usage, not all of them checked by ESLint, that make the learning curve a bit brutal. But yes, insanely useful once you get the hang of them, they enable forms of abstraction and composability that you'll sorely miss when working in other UI frameworks.
- radicalbyte 5y agoAren't there proper TypeScript bindings which catch (more) cases?
- judofyr 5y agoInteresting. I have the opposite experience, the more I use them the more clunky they feel. * If there’s only useState, then everything is mostly fine. All is good. * To avoid unnecessary re-rendering you’ll have to move callbacks into useCallback. These callbacks then also need to specify all of their dependencies. So many lines of code that are merely noise. * They encourage having state locally in the component which often breaks down the moment you slightly tweak the design. So much refactoring! * Dealing with any other (callback-based, stateful) API is confusing. If you want something to execute once initially you can use useEffect with an empty dependency list, but note that then there’s no way of accessing updates values (in callbacks) and everything will reflect the initial state. Often you’ll have to use useRef just to keep track of the current state. Or maybe useState? * Debugging is painful. You forget one value in your dependency list and the weirdest things happen. Suddenly you have callbacks running in different rendering scopes with different values. They feel very “brittle”: Once it works the code looks pretty, but when there’s a slight change of requirements you need to rethink everything.
- deergomoo 5y ago> To avoid unnecessary re-rendering you’ll have to move callbacks into useCallback. These callbacks then also need to specify all of their dependencies. So many lines of code that are merely noise. I really like Vue 3’s Composition API as a much easier-to-use (IMO) implementation of hooks. It’s conceptually very similar to React Hooks, but the method that defines them only runs once, when the component is instantiated. So straight away that removes any worries around conditionals and loops. Instead of a plain value/object and an update function, the “hooks” instead return a reactive object that tracks it’s own dependencies (which are also all reactive objects). That removes any need to manage dependencies yourself, or for any memoization. The only downside is that because a plain value (like a number or something) cannot be reactive, it has to be wrapped in an object and accessed via a “.value” property, but this is only relevant in the setup function. Anywhere else, like the Options API or in the template, accessing the value like a value will just get transparently proxied to the object.
- nightski 5y agoThe entire concept of reactive objects brings back memories of extreme pain from KnockoutJS days (and terrible performance in complex scenarios to boot). I prefer just working with plain old data structures.
- benatkin 5y agoI prefer working with just plain old setup functions, that are called once.
- bobthebuilders 5y agoI find React and Vue to be different expressions of the same core concept in a way. Althoguh I do prefer the functional nature of React, reactive objects are a lot less boilerplate, even if it might be a bit slower. The composition api itself is actually I think where it shines, because the options API is very mediocre in my opinion.
- 5y ago
- antjanus 5y agothat's pretty much how I feel, too. I really love the flow of hooks and I write a ton of them -- majority of the logic I write tend to end up in hooks and I really love that. There's something to having a function that can hold state or load its state on mount, etc.
- an_opabinia 5y agoUnsurprising it took people almost a decade to discover "depend()" from Meteor.
- kabes 5y agoReact hooks allows for reusable bits of logic. Reusable bits of logic are a great idea for a UI framework. But react hooks is a terrible implementation of that idea. You're no longer writing plain javascript, you're writing in some terribly verbose language where everything from variable declarations to function expressions needs to be wrapped, making it hard to integrate with this party APIs and small details can have a big impact on performance. Both vue 3 and svelte show it can be done better.
- jokethrowaway 5y agoIt's a good abstraction (definitely a massive improvement over the awkward classes) but it's built on so much react tech that you need limits which don't make sense (all the rules you need to remember) and when things break you're in for a world of pain. You can definitely have "hooks" that are just normal js functions without limits. My favourite public example of this is definitely solid.js (by ryansolid). In general, I'm grateful to react / facebook for bringing in the spotlight the hyperscript way of doing applications and pushing tons of developers to develop similar libraries - but technically react is bloated and a ugly corporate mess. Similar considerations for redux and elm.