Vue: Object reactivity caveat?

Created on 11 Feb 2017  路  5Comments  路  Source: vuejs/vue

Caveats

Due to limitations in JavaScript, Vue cannot detect the following changes to an array (...)

Is this the case for objects as well?

This fails:

state.sportType.sports[keyToAdd] = {...sportPrototype}

where sports is an object; and this passes:

Vue.set(state.sportType.sports, sportsLength, keyToAdd)

Most helpful comment

Yeah, it's not really the same thing.

  • For arrays, array[index] = 'something' can never be detected.
  • For objects, object['key'] = 'something' can be detected for existing properties - it's only a problem when trying to add new props this way (for which we have to use Vue.set())

All 5 comments

Yes, that's the case for objects as well. Whenever you add new properties to objects you should use Vue.set.

As @andreiglingeanu pointed out, yes. Next time, please ask questions on the forum or StackOverflow. The issues are for bugs and feature requests only

edit: It's the same if keyToAdd isn't present on the object, which is what I thought because of the name

Posted this here to specify that it should probably be updated in the List Rendering docs, which mention this caveat for arrays only. (Despite it being mentioned in Reactivity)

Array indices and Object properties are not the same thing.

Yeah, it's not really the same thing.

  • For arrays, array[index] = 'something' can never be detected.
  • For objects, object['key'] = 'something' can be detected for existing properties - it's only a problem when trying to add new props this way (for which we have to use Vue.set())
Was this page helpful?
0 / 5 - 0 ratings