When employing vue.set within Vuex, it seems that the state is not getting properly updated

Today, I encountered an issue while trying to add an object into a nested array. Despite successfully using similar code for other states, it does not seem to be working this time around.

I can't help but wonder if the problem lies in the fact that it is a nested array?

Here is the snippet of code I am currently using:

Vue.set(state.sections[getCurrentSection(state).index].rows[getCurrentRow(state).index].columns[getCurrentColumn(state).index].elements, 0, element)

Below is the structure of the element object being used:

var element = {
    id: id,
    style: {
        backgroundColor: {
            value: 'rgba(255,255,255,1)',
        },
    },
}

What could possibly be going wrong here?

Answer №1

UPDATE

Here is an alternative method for cloning:

function hasProp(arg1, arg2) {
  return Object.prototype.hasOwnProperty.call(arg1, arg2);
}

function extend(arg1, arg2) {
  const keys = Object.keys(arg2);
  const len = keys.length;

  let i = 0;

  while (i < len) {
    arg1[keys[i]] = arg2[keys[i]];
    i += 1;
  }

  if (hasProp(arg2, 'toString')) {
    arg1.toString = arg2.toString;
  }

  if (hasProp(arg2, 'valueOf')) {
    arg1.valueOf = arg2.valueOf;
  }

  return arg1;
}

const obj1 = {
  a: 1,
  b: 2,
  c: { a: 1, b: 2}
};

const cloned = extend({}, obj1);
cloned.a = 9999;

console.log(obj1, cloned);


  1. To create a deep copy of your state.sections:
  2. Generate a new object and apply the necessary modifications;
  3. Swap out state.sections with the new object -
    state.sections = Object.assign({}, newObject)
    .

Your state and view will be updated accordingly.

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

Custom label slots in q-file for the Quasar file picker for personalized file selection label

Can you provide guidance on how to properly display custom label slots in Quasar? I am looking to incorporate icons or images using the label slot. Below is my data(): data() { return { showLabel: true, labelText: "My custom Label& ...

What does the `Class<Component>` represent in JavaScript?

Apologies for the lackluster title (I struggled to think of a better one). I'm currently analyzing some Vue code, and I stumbled upon this: export function initMixin (Vue: Class<Component>) { // ... } What exactly does Class<Component> ...

The Vuetify asynchronous search feature is only able to send single characters at a time, and it doesn't concatenate the characters

In my Vue component, I have the following code: <template> <v-form ref="form" @submit.prevent="search"> <v-row class="pa-0"> <v-col cols="12" md="2" class="d-flex"> <v-autocomplete ...

Is it more beneficial to utilize multiple Vue apps or just one app per page?

Our website is primarily built using Razor/MVC, but we have been incorporating Vue gradually over the past few years. Currently, we have multiple Vue apps integrated separately into different sections of our site - such as modals, forms, and widgets in the ...

Align a button to the left or right based on its position within the layout

On the left side, there is dynamic text and on the right side, a button is displayed as shown below: <div class="dynamic-text"> {{ dynamicText }} </div> <div class="some-button"> < ...

When is the right time to develop a new component?

Exploring the strategy behind determining when to create a new component in a web application using angularjs / angular has been on my mind lately. It seems like this concept could apply to all component-based front end frameworks as well. I understand th ...

"Error: Unable to access the property '$emit' of an undefined value" - VueJS

I'm currently working on implementing a basic authentication system in vuejs. I have a set of objects containing valid usernames and passwords. I am looping through this list to validate the entered username and password. If there is a match, I trigge ...

Guide on building a multi-page application using Vue or React

I find myself a bit confused when it comes to single-page applications versus multi-page applications. While I am aware of the difference between the two, I am struggling with creating a MPA specifically. Up until now, I have built various apps using Rea ...

Having trouble with Vue component not showing updated data after axios POST request issue

Hi there, I'm facing an issue and could really use some guidance from a skilled Javascript Wizard. Here's the problem: I have a Laravel collection that I'm passing to a Vue component. Within the component, I am looping through the collecti ...

Guide to integrating a menu in Vue.js

I am currently facing an issue with importing a component from vue.js into my App.vue file. Here is the code for the component: <template> <h1> Hello</h1> </template> <script> export default { } </script> And here ...

Tips for resolving the issue of table rows becoming detached in Laravel when using Vue.js: How can I prevent my table rows from being split

I am facing an issue while trying to create a table using Vue.js and Laravel. The problem is that each row of the table data is being displayed as a separate table. Despite making changes to the code in both Vue and the controller, I cannot seem to resolv ...

Safari is not displaying the favicon even though multiple sizes have been provided

I've been attempting to display a favicon on Safari, but it seems to be working on all other browsers except for Safari. The application I developed is running on Nuxt3 with the composition api. In the default layout, I have included the following c ...

Testing Vue.js components - monitoring changes to props

I'm currently facing an issue while trying to unit test a watcher on a prop within a Vue component using Karma+Jasmine. It appears that the watchers on props are not being triggered during the unit test. Let's take a look at a simple example com ...

Steps for passing a layout property to a page component in Nuxt.js

I'm new to VueJS and NuxtJS, and I'm having trouble passing a property from a layout to a page component. Below is my layouts/default.vue <template> <Nuxt myprop="hello world" /> </template> <script> export ...

PHP and Vue component not displaying properly on webpage

I am having some trouble with my PHP code, as I am attempting to incorporate a Vue component into an HTML file. However, all I see is a blank page and there are no JavaScript errors appearing. Could someone please review the code below and let me know if ...

Loading dynamic components asynchronously in Vue 3 allows for improved performance and enhanced user experience

My goal is to dynamically display components based on their type. Here's how I'm approaching it: I have several similar components that should show different content depending on their type. Using the defineAsyncComponent method, I can easily im ...

How can I correctly implement a pinia store within another pinia store in Vue 3?

Within my todosStore pinia store, I am implementing CRUD operations against the backend. To achieve this functionality, I require a separate pinia store called bannerStore for updating the message and class of the bootstrap alert component in the template. ...

Exploring the magic of Plugins within Vue's Single File Components

One of my recent projects involved writing a custom plugin in Vue: var MyCoolVuePlugin = { install(Vue) { Vue.prototype.doStuff = function() { console.log("I'm a useless plugin") } } } export default MyCoolVuePlug ...

What is the best method to retrieve initial data in Vue 3 using async/await prior to rendering?

I am currently in the early stages of learning Vue 3 and I'm looking to fetch initial data from an API to populate certain fields before the App component is loaded. According to the [documentation][1], top-level await can be used within the <scrip ...

Limiting the table width in TinyMCE

Currently, I am utilizing TinyMCE within a Vue application. I've set up TinyMCE with the table_grid: false configuration, causing a dialog to appear when a table is created. This dialog allows users to specify rows, columns, width, and more. Within t ...