React Hooks have been on everyone's mind for a while now, and now that the hype has died down, I thought it would be nice to write a short introduction on them and go through concrete use-cases.
React Hooks have been introduced with React version 16.8, they allow us to use features that were once reserved to class components (such as internal state, lifecycle hooks etc.) in functional components.
This is great, as writing functional components is often preferred by the community as they offer advantages, namely: code that is easier to read and maintain, easier to test and often following better practices. For example, it's easier to create presentational, container and business logic functional components than it is using a class-based components.
Today, we'll only cover two of the most used hooks:
Make sure you have a grasp of React's fundamentals to take full advantage of this lesson, especially the concept of internal state and lifecycle hooks.
To follow along, feel free to clone the following repository where we'll transform class components into functional components using these two hooks. These components can be found under
Alright, we have the following class-based component:
All it does is allow the user to input something, which is saved in the components internal state and displayed bellow, like so:<div style="display: flex; align-items: center; justify-content: center"> <img width="50%" alt="render of the component mentioned" src="https://user-images.githubusercontent.com/15229355/57177519-cb69bc00-6e64-11e9-9871-5cf31f4c874e.png"> </div>
This component requires an internal state, so using a class-based approach made sense before 16.8, but the
useState hook will allow us to transform it into its functional counterpart.
useState syntax is very easy to grasp:
value is the variable to which we will bind the state,
setState is the method to be called to update it and the parameter passed to
useState is the state's default value. Pretty easy, right?
Transforming the component
Going from a class component to a functional one will take 2 easy steps:
- First, we change the declaration of the component into a functional one
- Let's now remove all traces of the class' context (this) and state
The final result
Alright, we can now use
useState with the syntax mentioned before to create an internal state.
Here's what the final component looks like (don't forget to import the hook):
For this example, we have the following component:
Where, on mount, we fetch a picture, save it in the internal state and display it, it looks something like this:<div style="display: flex; align-items: center; justify-content: center"> <img width="50%" alt="render of the component mentioned" src="https://user-images.githubusercontent.com/15229355/57177873-0110a400-6e69-11e9-820e-727f47c5192d.png"> </div>
The focal point being the lifecycle hook
componentDidMount that is called whenever the component is mounted (meaning whenever it is inserted into the DOM tree). We will use the
useEffect hook to do the exact same thing but in a functional component.
Once again, this hook's syntax is easy to understand and use:
It takes as its first parameter a callback that will be triggered each time the component is rendered.
But in our case, we only wish to trigger it once, when the component is mounted, right?
To do so, we can pass
useEffect a second parameter, an array of variables that will trigger the callback only when they are modified (instead of triggering it at every render of the component). We can also pass an empty array (
) to tell the callback to be triggered only on mount and dismount of the component, making it look like so:
Transforming the component
We'll skip this part, as it doesn't change much from the previous iteration.<br>
The final result
React Hooks are a great addition to the library, they provide considerable advantages and make the developer experience much smoother.
One important thing to note is that there are many other hooks, some more used than others and I invite you to read up on the official documentation as it is very well produced.
Other references include:
Thank you for reading, if you've learned something feel free to follow me on Twitter @christo_kade as I'll share all my new blog posts about React, Vue and the JS ecosystem as a whole ❤️