Skip to content

Latest commit

 

History

History
52 lines (35 loc) · 1.49 KB

no-store-to-refs-in-store.md

File metadata and controls

52 lines (35 loc) · 1.49 KB

Disallow use of storeToRefs inside defineStore (pinia/no-store-to-refs-in-store)

💼⚠️ This rule is enabled in the following configs: ✅ recommended, ✅ recommended-flat. This rule warns in the following configs: 🌐 all, 🌐 all-flat.

Rule Details

When stores are cross used, whichever store gets its use... called first will exists as a placeholder in the other store until its own setup function returns. That's why storeToRefs() do not work there and should be avoided altogether with cross used stores.

❌ Examples of incorrect code for this rule:

import { useUserStore } from './user'

export const useCartStore = defineStore('cart', () => {
  const { user } = storeToRefs(useUserStore())
  const list = ref([])

  const summary = computed(() => {
    return `Hi ${user.name}, you have ${list.value.length} items in your cart. It costs ${price.value}.`
  })

  function purchase() {
    return apiPurchase(user.id, this.list)
  }

  return { summary, purchase }
})

✅ Examples of correct code for this rule:

import { useUserStore } from './user'

export const useCartStore = defineStore('cart', () => {
  const { user } = useUserStore()
  const list = ref([])

  const summary = computed(() => {
    return `Hi ${user.name}, you have ${list.value.length} items in your cart. It costs ${price.value}.`
  })

  function purchase() {
    return apiPurchase(user.id, this.list)
  }

  return { summary, purchase }
})