Reactivity In Vue.js (And Its Pitfalls)

Anthony Gore
Aug 23, 2017 · 4 min read
Image for post
Image for post

One thing we love about Vue is the reactivity system. If we change a data value it triggers an update of the page to reflect that change.

For example:

Data properties, like in this example, are reactive, meaning they will trigger a re-render if changed.

Note: this article was originally posted here on the Vue.js Developers blog on 2017/03/06

Pitfalls of automatic reactivity configuration

Vue configures reactivity automatically whenever you create a data property, computed property, bind a prop etc. This automatic setup is great when coding an app because it:

  • Saves us time
  • Makes our code terse
  • Helps minimise our cognitive load

i.e. makes things simple. But this simplicity can come back to bite us! The pitfall is that, like an automatic car, automatic reactivity makes us lazy and when it doesn’t work we have no idea why!

When good reactivity goes bad

A student in the Ultimate Vue.js Developers course brought an interesting problem to me the other day. He was working on Vue.js Poster Shop, the first project of the course, which requires you to make a shopping cart using Vue.

A product being displayed in the shop is initially represented like this:

But when you add a product to the shopping cart you also need to a quantity, which he dealt with in a method like this:

The logic of the method is as follows:

  • Find the item in the shopping cart
  • If it’s there, increase the quantity
  • If it’s not there, give it a quantity of 1 and add it to the cart

A wild problem appears

The shopping cart template simply displays a list of cart items:

The problem was that no matter the value of , the template always showed its value as “1”.

My initial thought was that the logic of the function must be wrong. But after some debugging I discovered that the property was indeed being increased each time the method was called, so that wasn’t the issue…

How reactivity works under the hood

While causing a mild form of joy in most circumstances, Vue’s reactivity system can cause confusion and frustration when it doesn’t work like you expect.

This can largely be avoided if you understand how it works.

Getters and setters

The default behaviour for a Javascript object when accessed is to retrieve or modify the property in question directly. For example:

But when and pseudo properties have been defined, these functions override that default behaviour. Read more about getters and setters if you don’t know what I’m talking about.

When a Vue instance is created, each data property, component prop etc is traversed and getters and setters are added for each. These getters and setters allow Vue to observe changes to the data and trigger updates.

reactiveSetter()

So, coming back to our product object which looked like this when we defined it:

After Vue instantiates we can view this object in the console and see the getters and setters that Vue has defined on it:

Image for post
Image for post

These getter and setter functions have a number of jobs (check the source code), but one of the jobs of is to trigger a change notification which results in a page re-render!

Caveats

This is a brilliant system, albeit a fallible one. If you add (or delete) a property after Vue has instantiated (for example in a method or lifecycle hook) Vue does not know about it.

Look and see that although is defined on the object there are no getters/setters for it:

Image for post
Image for post

Updating reactive objects

In the shopping cart example, the way we solved the problem is to create a fresh object when adding to the cart rather than adding a property. That ensures that Vue has the opportunity to define reactive getters and setters:

Vue.set

But if you don’t want to create a new object you can use Vue.set to set a new object property. This method ensures the property is created as a reactive property and triggers view updates:

Arrays

Like objects, arrays are reactive and are observed for changes. Also like objects, arrays have caveats for manipulation. Vue wraps array methods like , etc so they will also trigger view updates.

This is not possible when directly setting an item with the index e.g.

Again, comes to the rescue:

Vue.set(app.myArray, index, newVal);

Vue.js Developers

Helping web professionals up their skill and knowledge of…

Anthony Gore

Written by

Here to teach you Vue.js! Course creator, Blogger, Vue Community Partner, creator of “Vue.js Developers”

Vue.js Developers

Helping web professionals up their skill and knowledge of Vue.js

Anthony Gore

Written by

Here to teach you Vue.js! Course creator, Blogger, Vue Community Partner, creator of “Vue.js Developers”

Vue.js Developers

Helping web professionals up their skill and knowledge of Vue.js

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store