Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vue & Vuex: no re-render after object key value mutation

I’m using vuex store value in front side

...mapGetters({ value : 'store/value' });

I’m facing a very strange situation:

  • If I update de store value like this then it re-renders and updated value appears on front side
UPDATE_VALUE: (state, value) => {
  state.value = value;
}
  • But if I try to update it like, so it mutates the store value but does not re-render
UPDATE_VALUE: (state, {key, value}) => {
  state.value[key] = value;
}

Do you have any idea ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Oh, That problem!

That’s the weird part of the reactivity system in vue 2, which I strongly recommend you to use vue 3, to avoid this. In version 3, proxies are far more better and understandable.

But, for solution, you can use Vue’s Vue.set method (or this.$set inside a component) to ensure that the change is reactive:

import Vue from 'vue';

UPDATE_VALUE: (state, {key, value}) => {
  Vue.set(state.value, key, value);
}

or if you like:

UPDATE_VALUE: (state, {key, value}) => {
  state.value = { ...state.value, [key]: value };
}

Both will work. Best of Luck!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading