Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Alex Suarez
Alex Suarez

Posted on

     

A guide for Skeletons in React.

Hi there developers! In this article, I will be sharing my experience in implementing skeletons components. Why using skeletons on loading states, how to implement them, and when and how to use them. Let's go.

Why

Skeletons or placeholders are very powerful for loading states, it's proven that reduce the user anxiety during the application loading process, and that is because it shows to the user the upcoming content structure(at least is intended), also gives the idea that the app is working, it is almost like "We have everything ready but the text and the images just wait a sec", so the user feels the app is about to show up the final content but structurally we already delivered the UI.

Implementation

In short, the easiest way to implement and skeleton screen is by using an SVG (using Figma for example) and add some animations to it, but if you have the time and the skills, you can create a responsive as a JSX component (using tailwindcss of course ) :)

HOC, In-component conditional rendering or composition

Well, it depends, I think HOC are meant for auth wrapper nowadays, since functional components and hooks solved many of the problems HOC use to solve in the past, with that said if you want to keep your JSX cleaner as possible, the way to go is... Composition of course. Let's take a look at it.

Let's create a simple skeleton component that just returns "loading..." when the "on" prop is true

functionSkeletons({children,on}){returnon?<p>Loading...</p> : <>{children}</>}
Enter fullscreen modeExit fullscreen mode

In your component...

importSkeletonsfrom'components/skeletons'exportdefaultfunctionApp(){// useFetch is a custom hooks for data fetchingconst[data,loading,error]=useFetch(URL)return(<Skeletonson={loading}><divclassName="App">{data.map(data=><divkey={data.id}>{data.name}</div>)}</div></Skeletons>);}
Enter fullscreen modeExit fullscreen mode

The above method is far better than creating the Skeleton component with no render condition on it.

functionSkeletons({children}){return<p>Loading...</p>}
Enter fullscreen modeExit fullscreen mode

An then handle the conditional rendering on every single component to show the Skeletons ...

importSkeletonsfrom'components/skeletons'exportdefaultfunctionApp(){// useFetch is a custom hooks for data fetchingconst[data,loading,error]=useFetch(URL)if(loading)return<Skeletons/>return(<Skeletonson={loading}><divclassName="App">{data.map(data=><divkey={data.id}>{data.name}</div>)}</div></Skeletons>);}
Enter fullscreen modeExit fullscreen mode

So compositions wins this fight, and if you ask me is the best way to implement Skeletons screens for your components loading states, it's easy to scalable and reusable.

So what's yours?

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Frontend Engineer
  • Location
    Amsterdam, Netherlands
  • Work
    Senior Frontend Engineer
  • Joined

More fromAlex Suarez

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp