Mathieu Barco
Mathieu Barco

Reputation: 91

vuejs boolean props watch doesn't trigger

I'm working on a vue cli project where items have two state equipped and unequipped.

This State is controlled by a Boolean located in the Props. Since you can switch the state I had to create a data isEquipped set to false by default.

I then added a watcher but it doesn't change my data value if my props is set to True.

Here's the code

name: "Item",
        props: {
            Index : Number,
            name: String,
            desc : String,
            bonus: Array,
            equipped  : Boolean
        },
        data() {
            return {
                isEquipped : false
            }
        },
        watch: {
            equipped: function(stateEquipped) {
                this.isEquipped = stateEquipped;
            },
        },

So for instance let's say I created a new item with equipped set to True, the watcher doesn't trigger and isEquipped stays at False, is there any reason to that ?

I came across multiple similar questions like this one Vue @Watch not triggering on a boolean change but none of them helped me

Upvotes: 4

Views: 7867

Answers (4)

chvolkmann
chvolkmann

Reputation: 524

Why not just use a computed value instead?

{
  // ...
  computed: {
    isEquipped () {
      // loaded from the component's props
      return this.equipped
    }
  }
}

You can then use isEquipped in your components just as if it was defined in your data() method. You could also just use equipped in your components directly as you don't transform it in any way.

<p>Am I equipped? - <b>{{ equipped }}</b></p>

Upvotes: 0

ZIS
ZIS

Reputation: 497

I am not sure what is the use case of isEquipped but seeing your code you can use the props directly unless there is a situation where you want to mutate the isEquipped that is not related to the props.

Upvotes: 0

Yash Maheshwari
Yash Maheshwari

Reputation: 2412

If you want to use watch then you can try define it as:

equipped: {
    handler () {
        this.isEquipped = !this.isEquipped;
    },
    immediate: true
}

This will change the value of this.isEquipped whenever the value of equipped will change.

Upvotes: 6

Iyashi
Iyashi

Reputation: 572

Watchers are "slow" and they operate on vue's next life-cycle tick which can result in hard to debug reactivity problems.

There are cases where you need them, but if you find any other solution, that uses vue's reactivity system, you should consider using that one instead.

The solution using a computed value from @chvolkmann probably also works for you.

There is a imho better way to do this:

export default {
  name: "Item",
  props: {
    Index : Number,
    name: String,
    desc : String,
    bonus: Array,
    equipped  : Boolean
  },
  data() {
    return {
      isEquipped : false
    }
  },
  updated () {
    if (this.equipped !== this.isEquipped) {
      this.isEquipped = this.equipped
      // trigger "onEquip" event or whatever
    }
  }
}

The updated life-cycle hook is called -as the name suggests- when a component is updated.
You compare the (unchanged) isEquipped with the new equipped prop value and if they differ, you know that there was a change.

Upvotes: -1

Related Questions