1. 5
  1.  

  2. 6

    That React Hooks can be confusing and frustrating is something even their creators share. However, once they click (no pun intended), I find them useful and not impossible to reason about. The author does a good job outlining some common head-scratchers, but the examples chosen don’t build a compelling case against their design.

    Take this for instance:

    What if the data we have has pagination links and we want to re-run the effect when the user clicks a link?

    The solution is already embedded in the definition of the effect — whenever the state (currentPage) changes, fetch its corresponding data. Therefore, on click … change the currentPage. (Or I am missing something glaringly obvious?)

    1. 2

      I think you’re right. I think it’s just that most developers (unless they have a lot of experience with reactive frameworks, such as RxJS) are not used to thinking of their data model as a dependency flow—or at least, not on a variable-by-variable level. For what it’s worth, I understand the author’a complaints; I just think Hooks are worth it, and my read is that, in the end, the author does, too.

    2. 1

      I recently paired with someone who had shot themselves in the foot with React Hooks. What I observed is this: hooks are much harder to form a mental model around. They seem like magic, so this person was using them in ways that don’t make sense, expecting this to work out magically. Once we started reasoning about a hook-owning component as a class component, this confusion cleared up. Classes are perhaps easier to reason about because there are many of analogues beyond React. That isn’t true of hooks. Most people, including myself, are surprised to see that something like that exists. They’re have a dubious necessity and they seem counterproductive to clear thinking.