Sitemap

React Hook Basics

4 min readFeb 5, 2020

--

If you follow React news it’s impossible that you haven’t heard about hooks. Released in February 2019 with patch 16.8.0, they spell out a very different direction for the future of the front end framework. You might be late to the game like me so I wanted to cover the basics in this article. Why were they created, how do I use them, and what do they mean for my current React projects? Let me start by clarifying — hooks are backwards compatible so you will not to be replacing any existing code!

At their essence, hooks are 10 special functions that allow us to take advantage of React’s features without writing classes. This comes with some big advantages. Overall, classes can be bulky, come with a steeper learning curve, and aren’t always needed. While learning React I often found myself unsure of when to use a functional component vs class component until it had been mostly built out, requiring a rewrite. This was disappointing because the framework seems to lean towards functional components.

From a practical standpoint, hooks produce leaner and faster code. Take a look at this example from a project I was working on.

Press enter or click to view image in full size
Simple class component
Same component using hooks. Don’t you love a good refactor?

This gets even worse when attempting to share logic between pre-hook components. With no inherent way to reuse stateful logic, developers had to find workarounds using render props and higher order components. This often ended up in what is called “wrapper hell”. Check out this example from Dan Abrimov’s twitter feed that a company had deployed.

Press enter or click to view image in full size
Fun stuff..

We all know the the simpler and more clear code is, the better, and that is what the underlying purpose of hooks gets at. Complex components become increasingly harder to understand because of the uncertain effects of state and lifecycle methods. They come with unnecessary logic which produces unexpected outcomes. At the same time this makes it more difficult to separate and simplify the components! Developers often use 3rd party libraries such as redux to take care of this problem. However, hooks have done such a job, there’s talk that it might replace them all together (I doubt this anytime soon). Let’s talk about how it all works.

useState hook

Before using any hook you must import it from React.

useState is the hook that you will be using to access state from within functional components. It declares a state variable, takes in one parameter (an initial state), and returns an array. The first value will be the state and the second value will be a function that allows you to change this state. useState is often assigned to a const that destructures the array, allowing you to name the variables whatever you would like:

Instead of calling {this.state.title} like before, using hooks you can just call {title}. If you would like to set up more than one state variable, simply call useState again! Easy stuff!

useEffect hook

useEffect combines three lifestyle methods (componentDidMount, componentDidUpdate, componentWillUnmount) into one method that runs when the page re-renders. useEffect takes in two parameters — a callback function that will execute on render, and a variable that will cause a render when it changes. This means that if the second parameter is an empty array, it will only render once!

Get David Fellows’s stories in your inbox

Join Medium for free to get updates from this writer.

There are some things to keep in mind for with useEffect. For methods that produce memory leakage (external subscriptions) an extra method should be passed in to clean it up. Also, just like useState, you can separate concerns and use useEffect multiple times! This way instead of grouping a large amounts of unrelated logic into one componentDidMount, you can create simpler functions.

Other hooks (and custom)

You may remember me saying that there are ten hooks. This is true but the rest are to handle edge cases and are not necessary for creating a React application. useContext and useReducer, for example, can be used abstract control of state similar to redux. They are worth exploring on your own and I encourage you to do so. A complete list can be found here.

Custom hooks are what allow us to extract reusable logic and store it in a separate file. You will be identify them because by convention they start with the word “use” and internally call one or more hooks. The name is important because it allows React to know that the “hook rules” apply to it.

Conclusion

Hooks are simple to implement and can make your code cleaner and more explicit. React will be trending towards using them instead of class components. To stay ahead of the crowd try building out a small project with hooks today!

--

--

David Fellows
David Fellows

Written by David Fellows

Optimistic Developer Based in NYC