Michael Hoffmann Profile picture
May 17 โ€ข 14 tweets โ€ข 6 min read Twitter logo Read on Twitter
๐Ÿ’ก Vue Tip: Effortless Handle Asynchronous Components With Suspense

@vuejs

๐Ÿงต ๐Ÿ‘‡๐Ÿป Image
@vuejs In the world of modern web development, delivering seamless user experiences is of utmost importance. Asynchronous components, which load data or resources dynamically, play a crucial role in achieving this goal.
@vuejs Vue 3 introduces an interesting feature called Suspense to effortlessly handle asynchronous components. You can use it to show a loading state while all async dependencies in the nested component tree are being resolved.
@vuejs โš ๏ธ <Suspense> is an experimental feature. It is not guaranteed to reach stable status and the API may change before it does.
@vuejs Without Suspense

For this article, let's use the following component hierarchy for demonstration purposes: Image
@vuejs We have a Dashboard component that includes two child components:

- Profile: This component is loaded asynchronously
- FetchComponent: This component fetches data using async/await within <script setup>
@vuejs Each of the child components has its loading state, they are visible if the component is loaded:
@vuejs As you can imagine this can harm the user experience if you see multiple loading spinners on the page and content displayed at different times.

Let's take a look at how <Suspense> is trying to solve that problem.
@vuejs The <Suspense> component enables us to display top-level loading & error states while waiting for nested async dependencies to be resolved: Image
@vuejs <Suspense> has two slots: #default and #fallback. Both slots only allow for one immediate child node.

Our demo application now behaves this way using <Suspense>:
@vuejs The <Suspense> component has two states: pending and resolve.

When <Suspense> is first rendered, it will display its default slot content in memory. If any asynchronous dependencies are encountered, it will transition to a pending state.
@vuejs While in the pending state, the fallback content will be shown. Once all the encountered asynchronous dependencies are resolved, <Suspense> enters a resolved state and displays the resolved default slot content.
@vuejs If no async dependencies were encountered during the initial render, <Suspense> will directly go into a resolved state.
@vuejs <Suspense> currently does not provide error handling via the component itself. However, you can use the errorCaptured option or the onErrorCaptured() hook to capture and handle async errors in the parent component of <Suspense>.

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Michael Hoffmann

Michael Hoffmann Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Mokkapps

Mar 1
๐Ÿ’ก Nuxt Tip: Use DevTools to Know Your App Better

@nuxt_js

๐Ÿงต ๐Ÿ‘‡๐Ÿป
โš ๏ธ Nuxt DevTools is experimental and under heavy development. APIs are subject to change.

You need Nuxt v3.1.0 or higher to use DevTools.
To enable Nuxt DevTools you can opt-in per-project by running the following command:
Read 13 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(