Search code examples
javascriptvue.jsvuex

vue life cycle method for updated vuex state


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?


Solution

  • 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.
        },
      }
    })