Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Preventing Infinite Render Loops from Inline Object Props

Tech Apr 29 13

Managing shared state between parent and child components requires strict attention to object reference stability. The following component architecture demonstrates how inline propp assignments can trigger a infinite rendering cycle:

<!-- DataTable.vue -->
<template>
  <div>
    <span>Current Filters: {{ queryConfig }}</span>
    <ul v-if="records.length">
      <li v-for="record in records" :key="record.id">{{ record.title }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  props: {
    queryConfig: { type: Object, default: () => ({}) },
    records: { type: Array, default: () => [] }
  },
  watch: {
    queryConfig() {
      this.records.length = 0
      this.fetchRemoteData()
    }
  },
  mounted() {
    this.fetchRemoteData()
  },
  methods: {
    fetchRemoteData() {
      setTimeout(() => {
        const payload = this.mockApiCall(this.queryConfig)
        payload.forEach(item => this.records.push(item))
      }, 15)
    },
    mockApiCall(cfg) {
      return [{ id: 1, title: 'Alpha' }, { id: 2, title: 'Beta' }]
    }
  }
}
</script>

Binding this component in a parent template with a inline object literal causes the UI thread to lock:

<!-- ParentContainer.vue -->
<template>
  <DataTable :query-config="{}" :records="itemStore" />
</template>

<script>
export default {
  data() {
    return {
      itemStore: []
    }
  }
}
</script>

The freeze originates from a recursive update chain. Vue's scheduler detects mutations to the records array and schedules a parent re-render. During template compilation, the inline :query-config="{}" syntax instantiates a fresh JavaScript object on every render pass. The child component's watcher evaluates the new object reference as a state change, clears the dataset, and initiates another network simulation. This cycle repeats continuously within the same macro-task.

Stabilizing the prop reference by declaring the configuration object inside the parent's reactive state breaks the loop:

<!-- ParentContainer.vue -->
<template>
  <DataTable :query-config="filterState" :records="itemStore" />
</template>

<script>
export default {
  data() {
    return {
      filterState: {},
      itemStore: []
    }
  }
}
</script>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

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