Files
inkreach-official-website/apps/website/.agents/skills/vue-options-api-best-practices/reference/stateful-methods-lifecycle.md
T
yeuimu 6c61a4e871 feat(deploy): production deployment setup and fixes
- Debian-based api image (bookworm-slim), docker/debian mirrors, prisma
  binaryTargets for openssl 3.0
- nginx: admin SPA under /admin, TLS via acme.sh (ZeroSSL) + auto-renewal
  cron, http->https redirect
- prisma: add origin_goods.delisted migration, sync missing schema
  (good_image/tag_font_color/good_tags), fix users.createdAt Timestamptz
- api: CORS wildcard reflection, helmet CORP cross-origin, price
  backfill in persistProductDetail, categoryIcon ancestor fallback,
  mediaByColor per-color gallery in public goods detail
- admin: /admin base path (vite + router)
- import-data.mjs: udt_name casting, serial sequence advance fix
2026-08-26 14:23:09 +08:00

1.9 KiB

title, impact, impactDescription, type, tags
title impact impactDescription type tags
Create Stateful Methods in Lifecycle Hooks MEDIUM Stateful functions like debounce/throttle in methods are shared across all component instances capability
vue3
options-api
debounce
throttle
lifecycle
cleanup

Create Stateful Methods in Lifecycle Hooks

Impact: MEDIUM - If you define debounced, throttled, or other stateful functions directly in the methods option, all instances of the component share the same function state. This causes race conditions and bugs in lists of components.

When a component is reused (e.g., in v-for), each instance needs its own debounced/throttled function. Define these in the created() hook and clean them up in unmounted() to prevent memory leaks.

Task Checklist

  • Never define debounced/throttled functions directly in methods
  • Create stateful functions in created() lifecycle hook
  • Always clean up (cancel timers) in unmounted()

Incorrect:

import { debounce } from 'lodash-es'

export default {
  methods: {
    // WRONG: All component instances share this debounced function!
    // If used in a v-for, clicking one button affects all instances
    handleClick: debounce(function() {
      this.performSearch()
    }, 500)
  }
}

Correct:

import { debounce } from 'lodash-es'

export default {
  created() {
    // CORRECT: Each instance gets its own debounced function
    this.debouncedSearch = debounce(this.performSearch, 500)
  },
  unmounted() {
    // CORRECT: Clean up to prevent memory leaks and stale calls
    this.debouncedSearch.cancel()
  },
  methods: {
    handleClick() {
      this.debouncedSearch()
    },
    performSearch() {
      // Actual search logic
    }
  }
}

Reference