From version 16.8.0, React introduced us to a way to use state and other React features without writing a class โ React Hooks.
Itโs an amazing improvement around the classic Class paradigm which allows us to reuse stateful logic between components. No surprise that it comes with a learning curve that could lead to performance pitfalls.
Letโs deep dive into the most popular ones and try to figure out how to avoid them.
Re-Renders Matter
Alright, we identified that we may encounter some performance issues while using Hooks, but where are they coming from?
Essentially, most of the issues with Hooks come from unnecessary renders of your components. Have a look at the following example:
This is a component that has two states, A and B, and four increment actions on them. Iโve added the console.log
method to see the message on every render. The first two actions are basic increments and just increase A or B values by one.
Letโs click on the a++, b++ button and have a look at the console: on each click, there should be only one render. This is really good because thatโs what we wanted.
Now press the a++, b++ after 1s button: on each click, youโd see two renders. If youโre wondering whatโs happening underneath โ the answer is simple.
React batches synchronous state updates into one.
On the other hand, for asynchronous functions, each setState
function triggers a render method.
But what if you want to have consistent behavior? Here comes the first rule of Hooks.
Rule 1: Do Not Split State Between Several useState Methods for Connected Data Models
Imagine you have two independent states. Then, the requirements changed, thus update of one state causes an update of another one.
In this case, you have to join them in one object: const { A, B } = useState({ A: 0, B: 0})
. Or, take advantage of the useReducer
function.
Another good example of this rule is data loading. Usually, you need three variables to handle it: isLoading
, data
, and error
. Donโt try to keep them separate, prefer useReducer
instead.
It allows you to separate state logic from components and helps you to avoid bugs. Having one object with these three properties will be a solution as well but would not be that explicit and error-prone.
Trust me on that, I have seen so many people forgetting to set isLoading: false
on error.
Custom Hooks
Now that weโve figured out how to manage useState
in a single component, letโs move increment functionality outside to be used in different places.
We refactored the increment logic to its own Hook and then we run it once using the useEffect
function.
Note that we have to provide the incrementA
setter in the dependency array because weโre using it inside and itโs enforced by Hookโs ESLint rules. (Please enable them if you didnโt do that before!).
If you try to render this component, your page will be frozen because of infinite re-renders. To fix it, we need to define the second rule of Hooks.
Rule 2. Make Sure You Return New Objects From Custom Hooks Only If Theyโve Changed
The component above is always re-rendering because the increment Hook returns a new function every time. To avoid creating a new function every time, wrap it in the useCallback
function.
Now itโs safe to use this Hook.
Sometimes, you need to return a plain object from custom Hooks, make sure you update it only when its content changes using useMemo
.
How to Find These Re-Renders Before Itโs Too Late?
Normally, itโs troublesome to find these issues before it causes performance issues, so you have to use specific tools to detect them beforehand.
One of them is the why-did-you-render
library that tells you about avoidable re-renders. Mark your component as MyComponent.whyDidYouRender = true
, start interacting with it, and look for messages in the console.
I guarantee that youโll discover something new in the next five minutes.
Another option is to use the Profiler tab in React Dev Tools extension. Although you have to think about how many re-renders you expect from your componentโ this tab only shows the number of re-renders.
Let me know what other challenges youโve encountered with Hooks, letโs solve them together.
Top comments (0)