Fading Coder

One Final Commit for the Last Sprint

Home > Tools > Content

Handling New Property Addition in Vue 2.x Without Triggering Re-render

Tools 1

The Problem with Direct Property Addition

Consider a Vue component with a v-for directive:

<p v-for="(value, key) in item" :key="key">
    {{ value }}
</p>
<button @click="addProperty">Add New Property</button>

With the following Vue instance:

const app = new Vue({
    el: "#app",
    data: () => ({
        item: {
            oldProperty: "Existing property"
        }
    }),
    methods: {
        addProperty() {
            this.item.newProperty = "New property"
            console.log(this.item) // Shows newProperty exists
        }
    }
})

The property gets added to the object but doesn't trigger a view update.

Technical Analysis

Vue 2.x uses Object.defineProperty for reactivity. When adding new properties dynamically, they aren't automatically made reactive:

const obj = {}
Object.defineProperty(obj, 'existingProp', {
    get() {
        console.log('Getter triggered');
        return val
    },
    set(newVal) {
        console.log('Setter triggered');
        val = newVal
    }
})

// This works with reactivity:
obj.existingProp
obj.existingProp = 'new value'

// This won't trigger reactivity:
obj.newProp = 'value'

Solutions

Vue.set()

Vue.set(targetObject, 'propertyName', value)

Intrenally, this calls defineReactive which uses Object.defineProperty to make the new property reactive.

Object.assign()

Create a new object with merged properties:

this.item = Object.assign({}, this.item, { 
    newProperty1: value1,
    newProperty2: value2 
})

$forceUpdate

As a last resort (not recommended for normal use):

this.$forceUpdate()

Recommendations

  • For adding few properties: Use Vue.set()
  • For adding multiple properties: Use Object.assign()
  • Avoid $forceUpdate() in most cases

Note: Vue 3's proxy-based reactivity system handles dynamic property additions automatically.

Related Articles

Efficient Usage of HTTP Client in IntelliJ IDEA

IntelliJ IDEA incorporates a versatile HTTP client tool, enabling developres to interact with RESTful services and APIs effectively with in the editor. This functionality streamlines workflows, replac...

Installing CocoaPods on macOS Catalina (10.15) Using a User-Managed Ruby

System Ruby on macOS 10.15 frequently fails to build native gems required by CocoaPods (for example, ffi), leading to errors like: ERROR: Failed to build gem native extension checking for ffi.h... no...

Resolve PhpStorm "Interpreter is not specified or invalid" on WAMP (Windows)

Symptom PhpStorm displays: "Interpreter is not specified or invalid. Press ‘Fix’ to edit your project configuration." This occurs when the IDE cannot locate a valid PHP CLI executable or when the debu...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.