Skip to content

Loading State #92

Closed
Closed

Description

@tlgimenes

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:

  1. 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
  2. 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

  1. lbebber commented on Jul 27, 2020

    @lbebber
    Contributor

    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.

    could you get further into this? specifically what you mean by "needs to understand all loading states of the ActualComponent"

  2. emersonlaurentino commented on Jul 27, 2020

    @emersonlaurentino
    Member

    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?

  3. lbebber commented on Jul 27, 2020

    @lbebber
    Contributor

    @emersonlaurentino I mean the other way around, why do you want to use loading inside the component, instead of before the component loads?

  4. tlgimenes commented on Jul 27, 2020

    @tlgimenes
    ContributorAuthor

    @lbebber, Imagine ActualComponent has the following implementation

    ActualComponent = () => (
    <div>...<div>
    <div>...<div>
    <div>...<div>
    )

    The implementation of Preview should be something like:

    Preview = () => (
    <Skeleton>
    <Skeleton>
    <Skeleton>
    )

    This means that the Preview component knows that ActualComponent renders 3 divs, softly coupling both components

  5. tlgimenes commented on Jul 27, 2020

    @tlgimenes
    ContributorAuthor

    Yes, @emersonlaurentino , that's the idea

  6. tlgimenes commented on Jul 27, 2020

    @tlgimenes
    ContributorAuthor

    @lbebber , the react-loading-lazy library provides an answer of why using loading state inside the component is a good idea

  7. lbebber commented on Jul 27, 2020

    @lbebber
    Contributor

    @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.

  8. tlgimenes commented on Jul 27, 2020

    @tlgimenes
    ContributorAuthor

    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.

  9. klzns commented on Jul 27, 2020

    @klzns
    Contributor

    Can we use both approaches?

    const ProductName = () => (
      <Suspense fallback={<ProductNamePreview/>}>
        <ProductNameFulfilled />
      <Suspense/>
    )
    
    const ProductPrice = () => (
      <Suspense fallback={<ProductPricePreview/>}>
        <ProductPriceFulfilled />
      <Suspense/>
    )
    
    
    const ProductPage = () => (
      <div>
        <ProductName />
        <ProductPrice />
      </div>
    )
  10. lbebber commented on Jul 28, 2020

    @lbebber
    Contributor

    @klzns That has the drawback of flickering while individual components load separately

  11. klzns commented on Jul 28, 2020

    @klzns
    Contributor

    If you want to coordinate the flickering of the ProductName and ProductPrice one could add a Suspense above them too

  12. linked a pull request that will close this issueContainer #267on Sep 1, 2020
  13. linked a pull request that will close this issueProduct Preview #268on Sep 1, 2020
  14. added a commit that references this issue on Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions