5

I am having a reactivity gotcha with Vue Vuex. I'm using an open source countdown timer. In the code below item.dueDate reacts properly when the active item changes in the $store, (the new date shows on the page), however, the data passed to Countdown doesn't update. It holds the old value. It does work the first time, though. So, it's not updating. Why not? Thanks!!

    <template>
        <v-layout>
            <v-flex>
                <v-card v-if="item">
                    <v-card-text>
                        <h3>Countdown {{item.name}} - {{item.dueDate}}</h3>
                    </v-card-text>
                    <Countdown v-if="item.dueDate" :deadline="item.dueDate"></Countdown>
                </v-card>
            </v-flex>
        </v-layout>
    </template>

    <script> 
    import Countdown from 'vuejs-countdown'

    export default {    
      components: { Countdown },
      computed: {
                activeItem(){
                return this.$store.getters.activeItem
            },
            item(){
                return this.$store.getters.loadedItem(this.activeItem)
            }
        }
    }
    </script>
6
  • I highly suggest you taking a look at this: vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats Commented Jan 4, 2018 at 22:58
  • Also, vuex note on vue's reactivity: vuex.vuejs.org/en/… Commented Jan 4, 2018 at 22:59
  • Thanks Derek -- I've reviewed these already but I can't quite put together why reactivity is working on the render part of the page, but not the Countdown.... ie. {{item.dueDate}} renders properly when it changes in the store. But <Countdown :deadline="item.dueDate"> does not re-render Commented Jan 4, 2018 at 23:34
  • 1
    I was able to work around this by modifying the Countdown component to include an updated() function... Commented Jan 5, 2018 at 0:01
  • 1
    mapGetters is just shorthand for creating a computed property from the store, but when there's only a couple, I just do em manually.... Commented Jan 5, 2018 at 13:34

1 Answer 1

2

The problem was with the Countdown module that I imported. When I opened it up, I saw why the component wasn't updating as expected.

The Countdown module sets the data only once on mounted

    mounted() {
        this.date = Math.trunc(moment(this.deadline) / 1000)
        }

So, if the component stays on the page as data changes, then it never updates again. I solved this by modifying the module to add an updated method, which did the trick...

    updated() {
        this.date = Math.trunc(moment(this.deadline) / 1000)
        }

Cheers, S

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.