Tips for initializing store data in vuex before the component is rendered

Currently, I am utilizing Vuex to store a tree structure within the state property.

This data is retrieved via an ajax call.

The issue arises when the page renders before the variable in the state property has been fully populated.

For example:

// sth.ts file
state: {
    map: {}
},
...
actions: {
    getData({ commit }){
        axios.get(...).then(... populate map ...).catch(...)
    }
}


// sthelse.vue file
<template>
<div>
<h1>Tree structure</h1>
<ul >
    <item v-for="child in map[root]"
             :key="child.id"
             :name="child.name"
             :children="child.Children">
    </item>
</ul>
</div>
</template>

I have conducted searches online but haven't come across a solution that addresses this specific issue. Has anyone encountered a similar problem before?

Is there any proven way to handle this situation effectively?

Answer №1

To keep track of the loaded map keys, store a reference in the vuex state as follows:

state: {
    map: {},
    keysLoaded: []
}

In the mutation that handles the data, add the key to keysLoaded. Then, in your component, create a computed property like this:

showTree () {
    return this.$store.state.keysLoaded.includes(this.root)
}

Only render the ul if showTree is true:

<ul v-if="showTree">

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

Issue with submitting a form within a React modal - lack of triggering events

I am utilizing the npm package react-modal (https://www.npmjs.com/package/react-modal) in my project. The issue I am facing is that when I click on 'Submit', nothing happens. The function handleSubmit</a> is not being triggered, as no conso ...

Utilize jQuery script on every single element

Is there a way to implement a jquery function on elements that are dynamically loaded via ajax? <span class="h">Test</span><br /><br /> <span class="h">Test</span><br /><br /> <span class="h">Test</ ...

Using JavaScript to transfer HTML entities as a function parameter

Is it possible to pass HTML entities as function parameters in JavaScript? I am developing a React application and I need to transmit HTML entities from the parent component to the child component as a property, and then exhibit the character represented ...

Having trouble adding HTML content to a parent div using jQuery and BackboneJS?

Utilizing Backbone Marionette to create a series of views, I am now faced with the task of making an AJAX call to my backend. In order to provide visual feedback to the user indicating that an action is being performed, I decided to integrate a spinner int ...

"Transferring information from Ruby to Javascript in Rails: A step-by-step guide

In my Rails 4 application, I am fetching data from the Last_fm API using HTTParty in one of my controllers. The user inputs a city, and I have designed an HTML page to display details of upcoming concerts in that city in a table format, including title, da ...

Troubleshooting JQuery Variable Overwriting Problem

Here is the code snippet I am working with: <script> $(document).ready(function() { var settings_image ={ url:"<?php echo site_url('/cms/editor/upload/images');?>", method: "POST", fileName: "file", returnType: ...

Prevent the use of harmful language by implementing jQuery or JavaScript

Is there a way for me to filter certain words (such as "sex") using regex, even when people use variations like "b a d", "b.a.d", or "b/a/d"? How can I prevent these kinds of words from getting through? I'm trying to filter more than just one word - ...

Comparing partial strings using Mongodb aggregation techniques

After populating my MongoDB database with around 5000 questions, I am now looking to group questions together that are partially similar, ranging from 70% to 80% similarity, and then send them to the frontend. I attempted to achieve this using the pipelin ...

In what way can I utilize the request object within a promise that is nested?

I am currently working on a Nuxt server side rendered application using the express framework for authentication with the openid-client package. My goal is to store the retrieved token in the express session, but I am facing an issue where the request mode ...

Unable to load dynamic data in Angular 2 smart table

Currently, I am utilizing Angular 6 along with smart table by visiting this link: . Everything was functioning smoothly, until the moment I attempted to switch from static to dynamic data: The following code works perfectly and displays all the content ...

Struggling with displaying nested JSON data in React components

I have a nested JSON structure running on my local server, and I'm using React to practice displaying this nested data. Currently, I am focusing on showing the day and the available time slots for that day. I have managed to extract the "days" (mon, t ...

When making an Ajax request to another website, the response received is in HTML format instead of

I am attempting to retrieve an array by making an AJAX GET request to a URL. Below is my controller code, which is hosted locally at localhost:3000 def merchant_ids merchants = Merchant.where(id: params[:id]).pluck(:merchant_name, :id, :merchant_city, ...

Is it appropriate to include a function within a loop?

I'm curious to know if it's considered good practice to call a function inside a loop. Both of the following code snippets produce the same result, but I want to add clarity by using a function. Is this considered good practice? Thank you. Code ...

Convert require imports to Node.js using the import statement

Currently learning NodeJs and encountering a problem while searching for solutions. It seems like what I am looking for is either too basic or not much of an issue. I am working on integrating nodejs with angular2, which involves lines of code like: impo ...

not getting any notifications from PHP

Despite receiving a status of '1' from this process file, my JavaScript code seems to be working fine. However, I am facing an issue with not receiving the email. <?php //Retrieve form data. //GET - user submitted data using AJAX //POST - in ...

Calling a JavaScript function from server-side code (without using startup scripts)

In essence, my objective is as follows: Initiate deletion of a record upon button click. Delete the record only if a specific column (Location) is null (working perfectly). If the specific column is not null, prompt the user for confirmation before proce ...

The issue arises when d3.scaleLinear returns NaN upon the second invocation

My journey with d3.js is just beginning and I'm taking it slow. Currently, I'm focused on creating a bar chart where data is loaded from a json file. When I click on the bars, the data changes to another column in the json. This is how my json f ...

Can the value of the currently selected option in a select box be obtained without needing to submit the form?

I'm currently working on a project and facing a roadblock with the following task: My goal is to extract the selected value from a dropdown menu and utilize it in my PHP script. The challenge here lies in capturing the chosen option without requirin ...

What is the best way to ensure a jQuery UI slider recognizes when the mouse is released after being clicked?

I have integrated the jQuery slider into my application and am facing an issue with triggering an event to update the database upon releasing the slider. Currently, I am using the following code snippet: $('.ui-slider-handle').mouseup(function () ...

Is the incorporation of state management tools like Redux or Vuex proven to enhance the speed of a web application?

Can storing data in a central store improve the speed of a web application? I believe one advantage is that after retrieving data from an API, it can be preloaded for the user when they revisit the view. In contrast, without state management, users would ...