Repository navigation
Description
Ok, so this is a big one.
Sooner or later, our app will have to deal with loading state. Currently, there are two main ways of dealing with loading state in your app. The first design pattern is to have a dedicated component that Previews the final state of another component. This design pattern is common in post-suspense era components, where you need to provide a fallback component to React's <Suspense/> component. An example is:
<Suspense fallback={<Preview/>}>
<ActualComponent/ >
<Suspense/>The problem with this approach is that the Preview component needs to be in sync with and needs to understand all loading states of the ActualComponent.
The second approach is to craft the loading state into the ActualComponent itself so that when it receives an isLoading prop ActualComponent renders it's loading state. This method is endorsed by react-loading-skeleton library.
After reading the react-loading-skeleton library, I tried to implement their pattern for previewing the product summary's price. The result was a noticeable reduction in TBT score.
This reduction happens mainly for two reasons:
- When adding loading state into the component, the loading state is rendered by the leafs in the react tree, what makes the browser to run all of the component's code on hydration. If we use the Preview component the react tree is shallower
- react-loading-skeleton computes and adjusts it's size to it's parent element, what adds more CPU intensive work to the first hydration
The conclusion we can get from these results is that we should try to make the loading state happens as early as possible for avoiding the browser to parse unnecessary JS. However, don't try making a complex loading state. It's ok to break the loading state into smaller, cohesive loading states.
Activity
lbebber commentedon Jul 27, 2020
could you get further into this? specifically what you mean by "needs to understand all loading states of the ActualComponent"
emersonlaurentino commentedon Jul 27, 2020
To use loading inside ActualComponent it needs to be called, so you can't use suspense. Consequently, the JS code will be larger. Is it a good one?
lbebber commentedon Jul 27, 2020
@emersonlaurentino I mean the other way around, why do you want to use loading inside the component, instead of before the component loads?
tlgimenes commentedon Jul 27, 2020
@lbebber, Imagine ActualComponent has the following implementation
The implementation of
Previewshould be something like:This means that the
Previewcomponent knows thatActualComponentrenders 3 divs, softly coupling both componentstlgimenes commentedon Jul 27, 2020
Yes, @emersonlaurentino , that's the idea
tlgimenes commentedon Jul 27, 2020
@lbebber , the react-loading-lazy library provides an answer of why using loading state inside the component is a good idea
lbebber commentedon Jul 27, 2020
@tlgimenes Alright, but do you need to render an exact preview? Wouldn't a generic preview suffice? I think this mostly a natural tradeoff between aesthetics and performance.
tlgimenes commentedon Jul 27, 2020
I think rendering a generic preview surface is a great idea. However I think you will still end up having to match the size of the Preview component to the ActualComponent and end up coupling both anyways.
klzns commentedon Jul 27, 2020
Can we use both approaches?
lbebber commentedon Jul 28, 2020
@klzns That has the drawback of flickering while individual components load separately
klzns commentedon Jul 28, 2020
If you want to coordinate the flickering of the ProductName and ProductPrice one could add a Suspense above them too
feat: Fix document in QuantitySelector (#92)