TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Hooks: React’s Do-Notation

84 pointsby graupabout 3 years ago

13 comments

jaredcwhiteabout 3 years ago
React has hooks because React&#x27;s &quot;classes&quot; were pretty crappy…partially due to crappy OOP in general in previous eras of ECMAScript. But instead of actually making their syntactical sugar better as JS&#x27; native OOP paradigm improved, they went the <i>opposite</i> direction. After having to use them in a large React codebase for years now, I still can&#x27;t stand them.<p>For a look at a UI library that actually rocks <i>precisely because it embraces</i> OOP and the native object graph of the DOM, check out Lit. It takes everything that&#x27;s awesome about browser-native web components, then simply adds some fabulous DX on top for reactive re-rendering upon prop updates. I&#x27;ve never used a UI library in my life that&#x27;s meshed with my brain better than Lit. I&#x27;ll take a good Lit-based web component class <i>any day</i> over React.
评论 #30497468 未加载
评论 #30499364 未加载
评论 #30498260 未加载
评论 #30497334 未加载
akdor1154about 3 years ago
My first boss had an often repeated mantra in code reviews: &quot;akdor, don&#x27;t fight the damn language!&quot;<p>If you&#x27;re using Python, write with Python concepts. In C#, write C#. In F#, write F#. Etc etc.<p>React has many many great ideas. But they are perpetually fighting the language. Here we have a great demonstration of stuff that is built in to any ml-legacy language being hacked into js in a way that the transformation happens in the user&#x27;s browser in a scripting language at runtime! It&#x27;s both genius and ridiculously inefficient and alien.<p>Don&#x27;t fight the damn language.
评论 #30496945 未加载
评论 #30498392 未加载
评论 #30497234 未加载
评论 #30497139 未加载
freindkabout 3 years ago
React breaks down because state comes in from multiple different directions and at different times - the history API has its own state, the query to the server has its own state, and the view has its own state, and they all happen at different times.<p>When a page changes in the history, the state of the view must change to reflect the history state, but when the user changes the state like going forward in pagination, the state in JavaScript is changed first, then the history state must be changed, then you have to figure out which state caused the change so not to get stuck in an infinite loop.<p>When a user initiates a new query, the query state changes, which triggers the request to the server, and then the results come back, but the query state may change when the results come back, which would trigger another infinite loop. Eg, when total_count is unknown, and then it becomes known with a query and must be included in the query state for optimisation, etc (don&#x27;t try to get total_count second time, etc).<p>That is the problem with React - state has timing issues. I hate it and don&#x27;t use it anymore. I just use pure JavaScript github.com&#x2F;thebinarysearchtree&#x2F;artwork. Half the code of react with orders of magnitude more performance just by using standard JavaScript stuff. It is hard to argue with that, but people will try until it can&#x27;t be denied.
评论 #30497871 未加载
captainmuonabout 3 years ago
I really don&#x27;t like hooks, because they are so magic. The old class based way of doing things was much more explicit. At least, hooks should take some kind of &quot;context&quot; and &quot;name&quot; parameters so that they are in spirit a pure function. Then you could also call them in loops and if blocks without problems.<p>Actually, class-based components have some annoying magic, too. The type of the object you create when you write `&lt;MyComponent&gt;` in JSX is different from the component class you write. There is some wrapper around it IIRC.<p>I wonder what React would look like if you get rid of all the cleverness and hidden state, and keep JSX and the reconciler as only magic?
评论 #30497627 未加载
评论 #30498562 未加载
评论 #30497625 未加载
评论 #30504072 未加载
评论 #30497651 未加载
jackblemmingabout 3 years ago
The functional programmers always sound like wannabe mathematicians to me, with their fancy category theory words. And the OOP guys always sound like wannabe engineers, with their fancy AbstractFactoryFactory.<p>Nice article though.
评论 #30498307 未加载
评论 #30498390 未加载
xixixaoabout 3 years ago
Interesting take, but I find it much more insightful to think about how you’d simply implement a hook (not any of the React specific ones). MobX uses similar mechanism.<p>user declares foo with a call to “useState”<p><pre><code> function useState(…) { useStateCalls.push(…) } useStateCalls = [] foo() &#x2F;&#x2F; do something with useStateCalls </code></pre> It’s a neat syntax sugar, it explains why they can’t be called conditionally, it allows for a clean api. If every component received a “hooks” argument, like this:<p><pre><code> function BazComponent(props, hooks) { hooks.useState(…) </code></pre> It would be less magic, more boilerplate (especially with custom hooks). That’s it really.
评论 #30496423 未加载
评论 #30497716 未加载
stephenabout 3 years ago
Really interesting to see the various examples&#x2F;incarnations the author went through to illustrate his point. Very well done.<p>I must admit I like his aphorism: &quot;If you&#x27;re honest while making your code better, you will inevitably end up making it functional&quot;.<p>Although I would tongue-in-cheek add a suffix of &quot;...you will inevitably end up making it functional, but stop once you start making everything a monad&quot;. :-D<p>I feel like FP purists hold &quot;...and now it&#x27;s a monad!&quot; as an forgone conclusion &#x2F; must be achieved end state. ...which, I did enough Scala for comprehensions (and now JS promises) that I believe I basically &quot;get&quot; monads, but so far I&#x27;ve not seen the light about the differences between &quot;good enough&quot; lazy abstractions (JS promises) and &quot;Shalt Follow All Of the Rules&quot; real monad abstractions, other than the latter end up letting you (sorry, tongue-in-cheek) write even more obtuse Haskell?...
bigyikesabout 3 years ago
The author discusses the sequential leaps towards functional programming: from mixins, to HOCs, to render props, and finally to hooks.<p>I’m very comfortable with hooks, but I’m not much of a functional programmer beyond that. So my question is: what is the next step in this progression? What is after hooks?
评论 #30496415 未加载
评论 #30496648 未加载
评论 #30496952 未加载
评论 #30496456 未加载
leecommamichaelabout 3 years ago
I always considered hooks to be a poor-man&#x27;s immediate-mode GUI, rather than some kind of pure FP thing. It&#x27;s not immediate-mode at all, but there are similar benefits to just being able to produce UI in the middle of the interactive loop.
eruabout 3 years ago
&gt; Well actually this is not an aphormism, it&#x27;s not even a propositional statement, because “X is a monad” is just math-speak for “X is composable”, just like “Y is a functor” is math-speak for “Y is mappable”.<p>Nah. Monads are one relatively special way to compose stuff. And monads don&#x27;t even compose very well.<p>To give example: the weaker applicative functors (to use Haskell&#x27;s terminology) compose much better.<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Applicative_functor" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Applicative_functor</a>
评论 #30497086 未加载
blindseerabout 3 years ago
I&#x27;m not well versed in React so only clicked into this article out of mild curiosity. If anyone is interested in discussing the non-technical aspects of this submission, please continue reading this comment :)<p>What do people think of the foray of these blog posts with embedded javascript components? It appears that this a nice way to provide interactive explanation on subjects. I think this post does a nice job. That said, the website experience is always a little off when someone uses components like this. For example, if you open the page and quickly scroll down, you get lots of flashes of the page layout changing. Also, the back history button seems to be broken after visiting this page. I think the website pushes itself to the browser history or something?
评论 #30496365 未加载
评论 #30496911 未加载
评论 #30497425 未加载
zamfiabout 3 years ago
*aphorism
mdomsabout 3 years ago
Sometimes I look at what&#x27;s going on in React land and just want to ask these developers, do you really need all this to make a UI? Really? Are you sure? Are you making things better?
评论 #30496752 未加载
评论 #30496537 未加载
评论 #30496627 未加载
评论 #30498250 未加载
评论 #30497011 未加载
评论 #30497660 未加载
评论 #30497675 未加载
评论 #30504897 未加载
评论 #30496515 未加载
评论 #30497291 未加载
评论 #30499990 未加载