Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

temo's avatar
Level 2

Vue 3 (Composition API) reactivity,refs, props

I have a Pinia store called 'categories' that looks like this:

export const useCategoryStore = defineStore('category', () => {
  const categories = ref([])

  async function fetchCategories() {
    try {
      const response = await axiosClient.get('api/categories')
      categories.value = response.data.data
    } catch (error) {
      console.log(error)
    }
  }

  return { categories, fetchCategories }
})

In one of my components, I am using this store to fetch data and retrieve the current category using a computed property. Here is the code:

<script setup>
const categoryStore = useCategoryStore()

const { categories } = storeToRefs(categoryStore)

if (categories.value.length === 0) {
  categoryStore.fetchCategories()
}


const currentCategory = computed(() => {
  return categories.value.filter((e) => e.id == props.category_id)[0]
})
</script>

And here happens thing I couldnt understand. when I

console.log(categories)

There's actual data with categories, but in proxy wrapped object, but if I

console.log(categories.value)

I'm getting empty proxy array. What's the reason of that? I can't retrieve nested values from that ref because of that (categories.value[0].id is undefined and etc.). How does this work and how to handle this kind of situations? (And exactly the same happens for currentCategory computed property. If i log currentCategory, it gives correct value but for currentCategory.value its undefined.)

0 likes
4 replies
temo's avatar
Level 2

@dacfabre

const categoryStore = useCategoryStore()

const { categories } = storeToRefs(categoryStore)

console.log(categories)

Here's output with actual data:

RefImpl {
  __v_isShallow: false,
  dep: undefined,
  __v_isRef: true,
  _rawValue: Array(0),
  _value: Proxy(Array)
}

dep: Set(1) {ReactiveEffect}
__v_isRef: true
__v_isShallow: false
_rawValue: (5) [{…}, {…}, {…}, {…}, {…}]
_value: Proxy(Array)
  [[Handler]]: Object
  [[Target]]: Array(5)
    0: {id: 5, name: {…}, description: {…}, hex: '#D33565'}
    1: {id: 4, name: {…}, description: {…}, hex: '#5CA844'}
    2: {id: 3, name: {…}, description: {…}, hex: '#6575B1'}

but with console.log(categories.value) I'm getting empty array, so I'm unable to pass this data with props or do any business logic on it

1 like
piljac1's avatar

There's a different behavior when logging an object vs. a primitive value. Object properties are references, so what happens when you log any object in the console (a Proxy is an object) is that it won't display the object property values at the moment the object was logged, it will display them when they're accessed (expanded in the console) instead. When logging a primitive value, you get the value at the moment you logged it.

My guess is that wherever you're console logging, your categories API call hasn't resolved yet and categories aren't set, but since object properties are by reference, the moment you open the console and expand the Proxy's Target value, the API call has resolved and you see your categories.

If you want to have the exact state of your Proxy object at the exact moment you logged it, the trick is to encode it into JSON. Here's an example:

console.log(JSON.stringify(categories, null, 2));
123123123's avatar

Hello. everyone! I hope you are doing well. Did you solve this problem? I'm just now facing the same problem as temo.

1 like

Please or to participate in this conversation.