TL;DRTo avoid having noticeable lag between re-renders, we should identify our expensive computations and pre-compute them before they reach our components.
Re-renders in React isn’t the devil. But it can be one when we dump all of our business logic there, including the most trivial data transformations.
The problem
Let’s examine a common scenario.
We get the backend response, an array of objects.
We map over this array in order to render a table
Each row of the table is a component that holds multiple sub-components
There shouldn’t be an issue, right? Our content changes only by applying filters, pagination, or sorting. All will trigger a fresh batch of data, so there won’t be a noticeable change.
But, what if we implement a functionality where we can select a range of items? For example, we can pick the first ten items, the last ten items, or the whole page. Here’s where things get tricky.
By clicking the 'select-all' checkbox in the header row, all the components will re-render. This is a very expensive operation.
The same applies for selecting a single row. The parent that holds the state will update and the rest of the components will re-render.
Oh no, it’s slow. It has always been.
No point thinking about these pesky re-renders. Our components were slow from the start.
Extracting expensive computations
We should dig deep into our components and look for the expensive computations.
Commonly the culprits are:
- Date parsing/formatting
- Nested loops
By formatting our data the moment we get them from the backend, (adding an adapter if you may), we guarantee two things:
- All the expensive computations are done on the get go, are co-located, easier tested and probably cached.
- Future incompatibilities with the backend will be fixed in a single place
Fin
That pretty much sums it up. Before going crazy about multiple rerenders, memoizing everything, we should take a step back and figure out if our components are doing too much.
For more digging on composition, re-renders and et al, I suggest following up on this great read: The mystery of React Element, children, parents and re-renders
Resources