vue life cycle method for updated vuex state

Viewed 76

I am using Vue and Vuex. In my component I have the following code:

computed: {
  ...mapState({
    address: state => state.wallet.address
  })
},

it works great in the UI, but what I want to do it to call a method as soon address changed its value. Any idea how I can trigger that?

2 Answers

watcher might help in your case. You can create a watcher to detect the change in address computed property. the watcher will be executed once the value changed. Refer to the example attached.

var vm = new Vue({
  el: '#demo',
  computed: {
    ...mapState({
      address: state => state.wallet.address
    })
  },
  watch: {
    address: function (newAddress, oldAddress) {
      // this watcher will be called once address changed
      // you can have access to previous and new values.
    },
  }
})

Mounted is the most-often used hook in the lifecycle. mounted() is called after DOM has been mounted so you can access the reactive component, templates, and DOM elements and manipulate them. In Server Side Rendering created()is used over mounted() because mounted() is not present in it.

---> mounted() <---

Related