Avoid displaying incorrect UI states while data is being loaded

Within my Vue application version 2.6.X, I utilize single-file components similar to the following structure:

<template>
  <div>
    <div v-if="users.length">
      <!-- users list rendering here -->
    </div>
    <div v-else>
      Warning: no users were found
    </div>
  </div>
</template>

<script>
import userService from '@/services/user-service'

export default {
  name: 'UserList',
  async created () {
    this.users = userService.loadUsersFromServer()
  },
  data () {
    return {
      users: []
    }
  }
}
</script>

An issue arises where a brief "flash of invalid UI state" occurs after component rendering but before the users are loaded. Are there any recommended methods to prevent this undesired flash?

One solution involves introducing a loading data prop, initializing it as true, then setting it to false once the AJAX request is complete. Subsequently adjusting the template as follows:

<template>
  <div>
    <div v-if="loading">
      Users are loading, please wait....
    </div>
    <div v-else-if="users.length">
      <!-- users list rendering here -->
    </div>
    <div v-else>
      Warning: no users were found
    </div>
  </div>
</template>

While functional in this particular scenario, if multiple requests need to be awaited before displaying the UI, this approach could become complex. Is there an alternative method that is more straightforward and efficient?

Answer №1

To efficiently handle multiple promises, consider employing the Promise.all() method before updating the loading status.

This approach is straightforward and is frequently my go-to method.

Alternatively, you can delay navigation until the data has been successfully retrieved if utilizing a router.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using PHP and DOJO to transfer HTML via JSON

My data store is currently querying a database and outputting JSON in the following format: $data[] = array('id' => $i, 'prod_id' => $product_id, 'link' => $link); I am curious about how to include a link using the ...

Utilizing JavaScript to analyze and interact with a website using Selenium's ghost drivers

Currently, I am attempting to scrape the second page of Google search results using Ghost driver. To achieve this, I am utilizing JavaScript to navigate through the HTML source of the search results page and click on the page numbers at the bottom with G ...

Customize Nuxt default document using a custom app.html file

According to the guidelines provided by Nuxt documentation, you have the ability to customize the default document by... placing an app.html file in the source directory of your project, which is typically the root directory. I followed these instructio ...

Fixed positioning of a div at a margin-top of 20 pixels while scrolling upwards

Seeking a solution for my div to remain fixed at 20px from the top of the window when scrolling up and 40px from the footer when reaching the bottom. My current code is inconsistent, is there a more efficient method available? Page Link $(document).scro ...

Transforming date and timezone offset into an isoDate format using moment.js

When retrieving data from the API, I encounter Date, Time, and Offset values in separate columns. My goal is to obtain an ISO date while maintaining the original date and time values. const date = "2019-04-15" const time = "13:45" const ...

What steps can be taken to repair the script?

https://jsfiddle.net/fnethLxm/10/ $(document).ready(function() { parallaxAuto() }); function parallaxAuto() { var viewer = document.querySelector('.viewer.active'), frame_count = 5, offset_value = 500; // init control ...

Is there a method to use media queries to dynamically switch JavaScript files based on the user's device?

I've been working on optimizing the mobile layout of my website, and while I have successfully implemented a media query with a different stylesheet for mobile devices, I'm struggling to determine if it's feasible to also load a separate Jav ...

Reactjs: Could someone provide a more detailed explanation of this statement?

After reading this line in the documentation here, I came across an interesting code snippet. this.setState({ chats: [...this.state.chats, data], test: '' }); It seems like we are adding to the 'chats' array in the state, but I&ap ...

Refresh an HTML page using Selenium WebDriver and Python without the need to actually refresh the entire

On a page with self-refreshing content (via WebSocket) similar to this example, the constantly changing content is not being detected by my Firefox webdriver. Although the new content is visible in the Firefox window, my webdriver only captures the initial ...

Unsuccessful attempt at testing RequireJS in a straightforward manner

As a beginner in RequireJS, I am currently experimenting to gain some experience. My goal is to make require load basic Angular first and then manually bring in Angular UI Bootstrap. However, I am encountering an issue where UI Bootstrap complains that ang ...

Setting attributes to a DOM element using String in jQuery

I am currently facing a challenge where I have a list of attributes saved as a string variable, and I need to add that variable to a <div>. Unfortunately, I am stuck and uncertain about the best approach. Here is what I have so far: HTML: <div&g ...

Storing Vue.js components as objects in a database: A step-by-step guide

Is there a way to serialize Vue.js components and store them in a database? For example, I am looking to save components like the HelloWorld component typically found in a fresh Vue installation. Any suggestions on a serialization process or package that ...

Server-side script for communicating with client-side JavaScript applications

Currently utilizing a JavaScript library that uses a JSON file to display content on the screen in an interactive manner. (::Using D3JS Library) While working with clients, it is easy to delete, edit, and create nodes which are then updated in the JSON fi ...

Filtering text for highlighting in Vue.js is a breeze

Struggling to create a text highlight filter using vuejs. The task involves iterating through an array of words, highlighting any matches with a span and class. However, I'm facing difficulty in getting the data to return with proper HTML formatting i ...

What is the best way to retrieve the value of an UL LI element using jQuery

I'm struggling to retrieve the value of a ul li listbox in a function, and I can't seem to figure out how. Here is my code: <ul class="dropdown-menu" id="newloc"> <?php $res_add = DB::table('res_br')-& ...

What are the most effective techniques for combining Vue.JS and Node.js backends into a single Heroku container?

I came across an interesting blog post today that discusses a method for deploying React applications. The author begins with a Node project and then creates a React project within a subfolder, along with some proxy configuration. About 10 days ago, I did ...

PDF viewing problem in iOS 11.3 when accessing PWA

I have successfully integrated Vuejs and Framework7 into my PWA. I am currently facing a challenge where I need to open a remote PDF file in my PWA while ensuring that users can easily navigate back to the PWA after viewing the PDF. The solution I initiall ...

Ways to clear a component's data

There are specific situations where I need to reset a component's data back to its initial values. To achieve this, instead of defining properties directly within the data() method, I use a function that returns a data object like so: data() { ret ...

"Troubleshooting: Why is my AngularJS ng-click not triggering the function

My custom directive fetches a navigation block from a webservice call. I am attempting to click on a navigation link that has an "ng-click" attribute set, which should call the specified function. However, the function is not being executed. Below is my r ...

Tips for including an additional label on a data point within a scatter plot using the Highcharts JavaScript framework

Incorporating the Highcharts JavaScript library, I am visualizing float values by passing them from PHP to the JS code. In the image below, you can observe that upon hovering over each point, the corresponding axes values are displayed along with the text ...