António Quadrado António Quadrado - 1 year ago 407
Javascript Question

How to update Vue component property when Vuex store state changes?

I'm building a simple presentation tool where I can create presentations, name them and add/remove slides with Vue js and Vuex to handle the app state. All is going great but now I'm trying to implement a feature that detects changes in the presentation (title changed or slide added/removed) and couldn't not yet find the right solution for it. I'll give the example only concerning the title change for the sake of simplicity. Right now in my Vuex store I have:

const state = {
presentations: handover.presentations, //array of objects that comes from the DB
currentPresentation: handover.presentations[0]

In my Presentation component I have:

export default {
template: '#presentation',
props: ['presentation'],
data: () => {
return {
shadowPresentation: ''
computed: {
isSelected () {
if (this.getSelectedPresentation !== null) {
return this.presentation === this.getSelectedPresentation
return false
hasChanged () {
if (this.shadowPresentation.title !== this.presentation.title) {
return true
return false
methods: mapActions({
selectPresentation: 'selectPresentation'
created () {
const self = this
self.shadowPresentation = {
title: self.presentation.title,
slides: []

self.presentation.slides.forEach(item => {

What I've done so far is to create a shadow copy of my presentation when the component is created and then by the way of a computed property compare the properties that I'm interested in (in this case the title) and return true if anything is different. This works for detecting the changes but what I want to do is to be able to update the shadow presentation when the presentation is saved and so far I've failed to do it. Since the savePresentation action triggered in another component and I don't really know how pick the 'save' event inside presentation component I fail to update my shadow presentation. Any thoughts on how I could implement such feature? Any help would be very appreciated! Thanks in advance!

Answer Source

I ended up solving this problem in a different way than what I asked in the question but it may be of interest for some. So here it goes:

First I abdicated from having my vue store communicating an event to a component since when you use vuex you should have all your app state managed by the vuex store. What I did was to change the presentation object structure from

    title: 'title',
    slides: []

to something a little more complex, like this

    states: [{
        hash: md5(JSON.stringify(presentation)),
        content: presentation
    statesAhead: [],
    lastSaved: md5(JSON.stringify(presentation))

where presentation is the simple presentation object that I had at first. Now my new presentation object has a prop states where I will put all my presentation states and each of this states has an hash generated by the stringified simple presentation object and the actual simple presentation object. Like this I will for every change in the presention generate a new state with a different hash and then I can compare my current state hash with the last one that was saved. Whenever I save the presentation I update the lastSaved prop to the current state hash. With this structure I could simple implement undo/redo features just by unshifting/shifting states from states to statesAhead and vice-versa and that's even more than what I intended at first and in the end I kept all my state managed by the vuex store instead of fragmenting my state management and polluting components.

I hope it wasn't much confusing and that someone finds this helpful. Cheers

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download