React Hooks — let’s not get too attached
Tags: webdev, react, javascript, frontenddev
Published: 2019-07-08 17:35:11

---
title: React Hooks — let’s not get too attached
published: true
tags: web-development,react,javascript,front-end-developmen
canonical_url: https://blog.logrocket.com/react-hooks-lets-not-get-too-attached-11b0ac09b4b5
---

### React Hooks — let’s not get too attached

![](https://cdn-images-1.medium.com/max/950/1*3BUc4O4T2cyE11h_HRxcXA.jpeg)

After the React team announced the proposal of a new feature called Hooks at React Conf 18, there was a strange reaction in the React community: a flurry of tutorials describing how to use it, as if it was already in React core.

What is explicitly described as [“a new feature proposal”](https://reactjs.org/docs/hooks-intro.html) was treated by some as a feature _announcement_. Clearly, there’s too much hype and “let’s rewrite everything right now” attitude, as Dan Abramov of the React team remarked:

<style>body[data-twttr-rendered="true"] {background-color: transparent;}.twitter-tweet {margin: auto !important;}</style>

> The content industry around React is both a blessing and a curse. It's amazing that many talented people create great articles and examples. But it creates unnecessary hype around an early stage proposal and makes people feel lost and confused. Spread love, not hype.
> 
>  — [@dan\_abramov](https://twitter.com/dan_abramov/status/1057027428827193344)

<script src="//platform.twitter.com/widgets.js" charset="utf-8"></script><script>function notifyResize(height) {height = height ? height : document.documentElement.offsetHeight; var resized = false; if (window.donkey && donkey.resize) {donkey.resize(height); resized = true;}if (parent && parent._resizeIframe) {var obj = {iframe: window.frameElement, height: height}; parent._resizeIframe(obj); resized = true;}if (window.location && window.location.hash === "#amp=1" && window.parent && window.parent.postMessage) {window.parent.postMessage({sentinel: "amp", type: "embed-size", height: height}, "*");}if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.resize) {window.webkit.messageHandlers.resize.postMessage(height); resized = true;}return resized;}twttr.events.bind('rendered', function (event) {notifyResize();}); twttr.events.bind('resize', function (event) {notifyResize();});</script><script>if (parent && parent._resizeIframe) {var maxWidth = parseInt(window.frameElement.getAttribute("width")); if ( 500 < maxWidth) {window.frameElement.setAttribute("width", "500");}}</script>

So let’s take a chill pill, not even bother with the proposed API, and ponder how React got to this point.

If you’d rather see the code and refactor your app to use Hooks right now, the [official docs](https://reactjs.org/docs/hooks-intro.html) are an excellent starting point.

In one of the [first conference talks](https://www.youtube.com/watch?v=x7cQ3mrcKaY) about React, Pete Hunt uses the word _hooks_ to refer to methods of React.Component, which allow the user to provide custom update logic — to _hook into_ React’s internals and tweak their behaviour. The new feature proposal is introducing _hooks_ in a similar sense — as a way of interacting with React by hooking your code into the React’s engine.

This idea of using functions instead of inheriting from classes plays well with the functional and declarative spirit of React. Because class-based components suffer from the old banana-gorilla-jungle problem, [as described](https://www.johndcook.com/blog/2011/07/19/you-wanted-banana/) by Joe Armstrong:

> the problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.

With React.Component, very often all you need is state or a ref, but what you get is a collection of methods you’ll never use and the necessity of writing the dreaded this keyword (which is an anagram for you know what). Basically, Hooks are a proposal to ditch the classes and instead of _inheriting from_ React, _hook into_ React.

[![](https://cdn-images-1.medium.com/max/1024/1*GPjaPKNNUYHU8EsA3Z0JGA.png)](https://logrocket.com/signup/)

### How to React without classes

The class-based API of React has been a problem for some time now. The ES6 classes themselves are not really classes (just syntactic sugar masking prototypal inheritance), they don’t compose well, and the usage of this keyword creates binding issues, especially when performing asynchronous operations.

So the search for class-less React development was on. Maybe the most notable project — now discontinued because of the introduction of Hooks — is [recompose](https://github.com/acdlite/recompose) (the first example in the docs is [providing a state to a function component](https://github.com/acdlite/recompose#lift-state-into-functional-wrappers)). However, recompose is making heavy use of the Higher Order Component pattern, which unfortunately creates a hard to read and false hierarchy in the render tree.

Another example is [Reactions](https://github.com/reactions/component)[Component](https://github.com/reactions/component), which uses another very popular pattern for making the class components more composable — Render Prop. The problem here is that a user first has to be pretty familiar with class-based React API to make some sense of how Reactions Component is to be used.

There are also cool projects that use the new [ES6 Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) to manage state, like [react-easy-state](https://github.com/solkimicreb/react-easy-state) or — in a more lightweight and experimental way —[react-recollect](https://github.com/davidgilbertson/react-recollect). I recommend the [article about the latter](https://hackernoon.com/a-different-way-to-manage-state-in-react-2d21dfb94482), which is a nice dive into how a super-readable and self-explanatory state management can be achieved. Sadly, the support for ES6 Proxies is [far from ideal](https://caniuse.com/#feat=proxy).

Currently, the Hooks implementation allows for replacing _almost_ all of class-based component’s functionality (Hook equivalents for componentDidCatch and getDerivedStateFromErrorare to be added soon), so after they’re added to React, the search can be called off. Classes in React aren’t going anywhere anytime soon, but it’s clear that React team envisions a class-free future for the library.

### Just one kind of component, please

The dichotomy between class and function components is an unnecessary burden for a user. I tend to regard a function component as a kind of lightweight little brother of the full-fledged class component — also, the distinction plays nicely with the [presentational and container component](https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0) pattern. But when I imagine learning React now, I think it’d be a bummer that a decision about using state or not (the most basic feature of React) has to be made before even starting to write the component.

It’s a bit like [synchronous vs. asynchronous functions](http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/): async/await and Promises help us escape callback hell, but why should a programmer have to care if an operation is async or not in the first place? For me at least, Hooks are a much more understandable abstraction than class Something extends React.Component— how do I know what’s lurking in the jungle that I have to bring along with the setState functionality?

### Even more declarative and functional future

React prides itself on being declarative, but using lifecycle methods is quite imperative — a classic example is duplicated code in componentDidMount and componentDidUpdate methods. Using Hooks, that can be achieved with useEffect function (there’s a great example of how Hook’s declarative API beats lifecycle methods’ imperativeness [in the official documentation](https://reactjs.org/docs/hooks-effect.html#explanation-why-effects-run-on-each-update)).

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FevcWYwGLBOaac%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FevcWYwGLBOaac%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FevcWYwGLBOaac%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7b6a20f7292666c4548486f54583fa9/href">https://medium.com/media/e7b6a20f7292666c4548486f54583fa9/href</a></iframe>

With Stateless Functional Components, React moved more in the direction of functional programming. Hooks are a move even further in that territory, as they enable building full-featured React apps without the use of classes, but with functions only.

When the proposal is finalized, it will be the biggest change to how React apps are developed since the launch of the library. Today’s nifty patterns like Higher Order Components or Render Prop will seem quirky or even hacky (because they introduce a false hierarchy). The familiar class App extends React.Component with its list of lifecycle methods will be a thing of the past, and if it wasn’t for JSX syntax, a piece of code might not even look like a React component at a first glance.

But until then, let’s not get too attached to the proposed API and focus on writing code using cool, stable features.

#### Further reading

- [The official Hooks proposal docs](https://reactjs.org/docs/hooks-intro.html)
- [Formatted RFC](https://github.com/reactjs/rfcs/blob/hooks-rfc/text/0000-react-hooks.md)
- [Dan Abramov’s introductory article](https://dev.to/ben/making-sense-of-react-hooks-2in4-temp-slug-3153604)

### Plug: [LogRocket](https://logrocket.com/signup/), a DVR for web apps

[![](https://cdn-images-1.medium.com/max/1024/1*s_rMyo6NbrAsP-XtvBaXFg.png)](https://logrocket.com/signup/)<figcaption><a href="https://logrocket.com/signup/">https://logrocket.com/signup/</a></figcaption>

[LogRocket](https://logrocket.com/signup/) is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single page apps.

[Try it for free.](https://logrocket.com/signup/)

* * *